Closed etcware closed 1 year 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
@etcware Thank you for addressing this issue. It is fixed as part of https://github.com/4spacesdk/CI4OrmExtension/releases/tag/1.0.4.
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
@etcware I believe I've found the issue and committed a fix. Let me know if it works with the newest version dev-master.
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:
ContentModel.php:
In NavigationController.php doesn't work the related get,
The wrong generated query is in the following and it doesn't take into account table / relation configuration:
The mysqli error is:
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.