4spacesdk / CI4OrmExtension

OrmExtension for CodeIgniter 4
MIT License
50 stars 9 forks source link

I update CI4OrmExtension to the last version and I have a regression with an old error #22

Closed etcware closed 1 year ago

etcware commented 2 years ago

I'm using the new version with an existent database where I cannot implement naming rules and there is the same problem of #21, all relations doesn't work. For example: NavigationModel.php:

protected $table = 'ptv_navigation';
protected $primaryKey = 'navigation_id';
...
public $hasMany = [
        'cont' => [
            'class' => ContentModel::class,
            'joinTable' => 'ptv_navigation_contents',
            'otherField' => 'nav',
            'joinSelfAs' => 'navigation_fk',
            'joinOtherAs' => 'content_fk'
        ],
...

ContentModel.php:

protected $table = 'ptv_contents';
protected $primaryKey = 'content_id';
...
public $hasMany = [
        'nav' => [
            'class' => NavigationModel::class,
            'joinTable' => 'ptv_navigation_contents',
            'otherField' => 'cont',
            'joinSelfAs' => 'content_fk',
            'joinOtherAs' => 'navigation_fk'
        ]

In NavigationController.php doesn't work the related get,

$navigation->conts->find();

The wrong generated query is in the following and it doesn't take into account table / relation configuration:

SELECT `ptv_contents`.*
FROM `ptv_contents`
LEFT OUTER JOIN `ptv_navigation_contents` `navs_ptv_navigation_contents` ON `ptv_contents`.`content_id` = `navs_ptv_navigation_contents`.`content_fk`
WHERE `navs_ptv_navigation`.`navigation_id` = 181

The mysqli error is:

$e mysqli_sql_exception (8) "Unknown column 'navs_ptv_navigation.navigation_id' in 'where clause'"

PS I edit this issue to add that I used the dev-master version where the #21 was fixed and now I use the 1.0.3 version.

etcware commented 2 years ago

I add more information: I get the previous version of CI4OrmExtension and the example in the previous post in this thread is working (it was resolved by fixing #21). But there is one new relation that doesn't. The new is a mix of naming rules: one parent table follows naming rules, the other not, the relation table not completely. Here the not working example, also with the old version: CategoryModel.php:

protected $table = 'categories';
protected $primaryKey = 'id';
...
public $hasMany = [
'cat' => [
            'class' => CategoryModel::class,
            'joinTable' => 'categories_contents',
            'otherField' => 'cont',
            'joinSelfAs' => 'content_fk',
            'joinOtherAs' => 'category_fk'
        ],

ContentModel.php:

protected $table = 'ptv_contents';
protected $primaryKey = 'content_id';
...
public $hasMany = [
...
        'cat' => [
            'class' => CategoryModel::class,
            'joinTable' => 'categories_contents',
            'otherField' => 'cont',
            'joinSelfAs' => 'content_fk',
            'joinOtherAs' => 'category_fk'
        ],

The resulting select query:

$content->cats->find();

is in the following:

SELECT `categories`.*
FROM `categories`
LEFT OUTER JOIN `categories_contents` `conts_categories_contents` ON `categories`.`id` = `conts_categories_contents`.`category_fk`
WHERE `conts_ptv_contents`.`content_id` = 106191
Martin-4Spaces commented 2 years ago

@etcware Thank you for addressing this issue. It is fixed as part of https://github.com/4spacesdk/CI4OrmExtension/releases/tag/1.0.4.

etcware commented 2 years ago

Thanks, the old relation are working. But in the thread I put in evidence that a new relation between contents and categories doesn't. I review the configuration and the it is in the following:

class CategoryModel extends Model
{

public $hasMany = [
        ...
        'content' => [
            'class' => ContentModel::class,
            'joinTable' => 'categories_contents',
            'otherField' => 'categ',
            'joinSelfAs' => 'category_fk',
            'joinOtherAs' => 'content_fk'
        ],
    ...
class ContentModel extends Model
{
...
public $hasMany = [
        ...
        'categ' => [
            'class' => CategoryModel::class,
            'joinTable' => 'categories_contents',
            'otherField' => 'content',
            'joinSelfAs' => 'content_fk',
            'joinOtherAs' => 'category_fk'
        ],
...

When I try to get related items from contents, the previous are working and categories are not:

try {
            $content->sects->find();
            $content->imgs->find();
            $content->navs->find();
            $content->atts->find();
            $content->categs->find();
        } catch (\Exception $e) {
            dd($content->categs->_getModel()->getLastQuery()->getQuery());
        }

I get mysql error because the query is this and the table names are wrong:

SELECT `categories`.*
FROM `categories`
LEFT OUTER JOIN `categories_contents` `contents_categories_contents` ON `categories`.`id` = `contents_categories_contents`.`category_fk`
WHERE `contents_ptv_contents`.`content_id` = 108524
Martin-4Spaces commented 2 years ago

@etcware I believe I've found the issue and committed a fix. Let me know if it works with the newest version dev-master.