Cakephp 3不会重现相关模型的记录

时间:2017-09-03 12:17:59

标签: php cakephp cakephp-3.x cakephp-3.4

我的学校模型与many to many表加入的属性模型有attribute_school关系。 在我的 Shell 文件夹中,我的主要功能是

$schoolsRaw = TableRegistry::get('Schools');

$schools = $schoolsRaw->find('all', [
    'fields' => [
        'Schools.name',
        'Schools.logo',
        'Schools.discount'
    ],    
    'contain' => [
        'Users'=> [
            'fields' => [
                'Users.first_name',
                'Users.last_name',
                'Users.email',
                'Users.title'
            ]
        ],
        'Attributes' => [
            'fields' => [
                'Attributes.model'
            ]
        ]
    ]
]);

echo debug(json_encode($schools , JSON_PRETTY_PRINT));

以上echo命令会为学校记录的所有实例生成空值,如下所示:

{
    "name": "Genesis School",
    "logo": "badges\/genesis.jpg",
    "discount": "Not Stated",
    "attributes": [],
    "user": {
        "first_name": "xxxxx",
        "last_name": "yyyyy",
        "email": "mynane@yahoo.co.uk",
        "title": "Ms"
    }

我的模特

//schools
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('schools');
    $this->setDisplayField('name');
    $this->setPrimaryKey('id');

    $this->belongsTo('Users', [
        'foreignKey' => 'user_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsToMany('Attributes', [
        'foreignKey' => 'school_id',
        'targetForeignKey' => 'attribute_id',
        'joinTable' => 'attributes_schools'
    ]);
}

// attributes model
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('attributes');
    $this->setDisplayField('name');

    $this->belongsToMany('Schools', [
        'foreignKey' => 'attribute_id',
        'targetForeignKey' => 'school_id',
        'joinTable' => 'attributes_schools'
    ]);
}

// join table
public function initialize(array $config)
{
    parent::initialize($config);
    $this->primaryKey('id');
    $this->setTable('attributes_schools');

    $this->belongsTo('Schools', [
        'foreignKey' => 'school_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Attributes', [
        'foreignKey' => 'attribute_id',
        'joinType' => 'INNER'
    ]);
}

这是cakephp 3.4,我不知道为什么它没有获取属性记录,也没有记录错误。请帮忙

0 个答案:

没有答案