目标:人可以关注任何其他人或组织。
/**
* PersonFollow schema:
CREATE TABLE `person_follows` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`person_id` int(11) DEFAULT NULL,
`table_id` int(11) DEFAULT NULL,
`table_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `i_person_id` (`person_id`),
KEY `i_table_id` (`table_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
*/
class PersonFollow extends AppModel {
public $belongsTo = array(
'Person' => array(
'foreignKey' => 'person_id',
),
'FollowPerson' => array(
'className' => 'Person',
'foreignKey' => 'table_id',
//'conditions' => array('PersonFollow.table_name' => 'persons'), // tried this
),
'FollowOrganization' => array(
'className' => 'Organization',
'foreignKey' => 'table_id',
//'conditions' => array('PersonFollow.table_name' => 'organizations'), // tried this
),
);
}
class Person extends AppModel {
public $hasMany = array(
'Following' => array(
'className' => 'PersonFollow',
'dependent' => true,
),
'Follower' => array(
'className' =>'PersonFollow',
'foreignKey' => 'table_id',
'conditions' => array('Follower.table_name' => 'persons'),
'dependent' => true
),
);
}
class Organization extends AppModel {
public $hasMany = array(
'Follower' => array(
'className' =>'PersonFollow',
'foreignKey' => 'table_id',
'conditions' => array('Follower.table_name' => 'organizations'),
'dependent' => true
/* Tried this
'foreignKey' => false,
'finderQuery' => 'SELECT `PersonFollow`.*
FROM `person_follows` as `PersonFollow`
WHERE `PersonFollow`.`table_id` = {$__cakeForeignKey__$}
AND `PersonFollow`.`table_name` = "organizations"',
*/
)
);
}
$following = $this->Person->find('all', array(
'contain' => array('Following' => array('FollowPerson','FollowOrganization')),
'conditions' => array('Person.id' => 1234),
));
$following
的结果返回all在table_id上匹配之后,并且不使用table_name条件。我正在尝试使用FollowOrganization,其中PersonFollow.table_name ='organizations',对于FollowPerson,PersonFollow.table_name ='persons'也是如此。似乎$hasMany
仅使用'foreignKey'而不是'条件'。我错过了什么?