我需要将模型加入自身...但我收到错误“不是唯一的表/别名:'图像'”
Image
架构:
id: int
thumbnail_image_id: int null
key: varchar... filename
location: varchar... s3 or local
created: datetime
modified: datetime
Image
模型:
<?php
App::uses('AppModel', 'Model');
class Image extends AppModel {
public $belongsTo = array(
'Thumbnail' => array(
'className' => 'Image',
'foreignKey' => 'thumbnail_image_id'
)
);
public $hasOne = array(
'Thumbnail' => array(
'className' => 'Image',
'foreignKey' => 'thumbnail_image_id'
)
);
public function exampleFunction() {
return $this->find('all', array(
'contain' => array('Thumbnail')
));
}
运行exampleFunction
它会给我错误“不是唯一的表/别名:'图像'”。CakePHP 像这样构建 SQL:
SELECT `Image`.`id`, `Thumbnail`.`id` FROM `images` AS `Image` LEFT JOIN `images` AS `Image`.`thumbnail_image_id` = `Thumbnail`.`id`;
但它应该是这样的......对吧?(注意倒置ON
):
SELECT `Image`.`id`, `Thumbnail`.`id` FROM `images` AS `Image` LEFT JOIN `images` AS `Thumbnail`.`id` = `Image`.`thumbnail_image_id`;
如果我在 mysql 控制台中运行上面的两个 SQL 查询,则第二个有效。
建议?