如何使用Cakephp进行查询联接?

时间:2018-08-19 15:07:52

标签: cakephp

我有评论表,用户可以在其中评论另一个用户,如下所示: enter image description here

这些是约束: enter image description here

当我使用此查询时:

$comments = TableRegistry::getTableLocator()
            ->get('Comments')
            ->find('all');
 $query = $comments
          ->find('all')
          ->contain(['Users']);

它检索注释,但仅在commented_id上加入。 我想检索包含其两个相关用户的注释对象 ,其中一个作为注释者,另一个作为注释,那么如何建立查询呢?

这是CommentTable:

class CommentsTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
public function initialize(array $config)
{
    parent::initialize($config);

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

    $this->belongsTo('Users', [
        'foreignKey' => 'commentator_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Users', [
        'foreignKey' => 'commented_id',
        'joinType' => 'INNER'
    ]);
}

/**
 * Default validation rules.
 *
 * @param \Cake\Validation\Validator $validator Validator instance.
 * @return \Cake\Validation\Validator
 */
public function validationDefault(Validator $validator)
{
    $validator
        ->integer('id')
        ->allowEmpty('id', 'create');

    $validator
        ->scalar('content')
        ->maxLength('content', 255)
        ->requirePresence('content', 'create')
        ->notEmpty('content');

    $validator
        ->integer('score')
        ->requirePresence('score', 'create')
        ->notEmpty('score');

    $validator
        ->dateTime('created_at')
        ->requirePresence('created_at', 'create')
        ->notEmpty('created_at');

    $validator
        ->dateTime('updated_at')
        ->requirePresence('updated_at', 'create')
        ->notEmpty('updated_at');

    return $validator;
}

/**
 * Returns a rules checker object that will be used for validating
 * application integrity.
 *
 * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
 * @return \Cake\ORM\RulesChecker
 */
public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->existsIn(['commentator_id'], 'Users'));
    $rules->add($rules->existsIn(['commented_id'], 'Users'));

    return $rules;
}

}

2 个答案:

答案 0 :(得分:1)

将您的人际关系调整为类似这样的方式:

let fadeOut = CABasicAnimation(keyPath: "opacity")
fadeOut.fromValue = 1
fadeOut.toValue = 0
fadeOut.duration = 1

let expandScale = CABasicAnimation()
expandScale.keyPath = "transform"
expandScale.valueFunction = CAValueFunction(name: kCAValueFunctionScale)
expandScale.fromValue = [1, 1, 1]
expandScale.toValue = [3, 3, 3]

let fadeAndScale = CAAnimationGroup()
fadeAndScale.animations = [fadeOut, expandScale]
fadeAndScale.duration = 1

因此,您可以包含两个不同的关系。

$this->belongsTo('Commenters', [
    'className' => 'Users',
    'foreignKey' => 'commentator_id',
    'joinType' => 'INNER'
]);
$this->belongsTo('Users', [
    'foreignKey' => 'commented_id',
    'joinType' => 'INNER'
]);

像这样访问它们:

$comments = TableRegistry::getTableLocator()
        ->get('Comments')
        ->find('all')
        ->contain(['Commenters', 'Users']);

答案 1 :(得分:0)

我不确定确切输出什么,但是据我了解,您可以对它使用自定义连接:

$data = $this->Comments->find()
                        ->join([
                          'Users' => [
                                          'table' => 'users',
                                          'type' => 'INNER',
                                          'conditions' => [
                                              'Users.id=Comments.commentator_id',
                                           'OR' => [
                                                'Users.id=Comments.commented_id'
                                                   ]
                                            ] 
                                     ]
                              ]);

或者您可以根据需要进行修改。

https://book.cakephp.org/3.0/en/orm/query-builder.html#adding-joins

希望这会有所帮助。