我是Cake用户的开始,但是精通php和框架工作(我以前使用Code Igniter)。如何调用下面的模型,只返回艺术家记录和相关的ArtistImage记录,而不是相册记录。
class Artist extends AppModel {
var $name = 'Artist';
var $hasMany = array('Album', 'ArtistImage');
}
另外,你能澄清一下$ this-Artist->递归的值吗?
感谢您的帮助
答案 0 :(得分:3)
// Artist /app/models/artist.php
class Artist extends AppModel {
var $name = 'Artist';
var $hasMany = array('Album', 'ArtistImage');
var $actAs = array('Containable');
}
// ArtistsController /app/controllers/artists_controller.php
class ArtistsController extends AppController {
var $name = 'Artists';
public function example() {
debug( $this->Artist->find( 'all' );
debug( $this->Artist->find( 'all', array(
'recursive' => 0,
));
debug( $this->Artist->find( 'all', array(
'contain' => array(
'ArtistImage' => array(
// artist find options here i.e. fields, conditions, order etc
),
);
));
}
}
// results
// with just find( 'all' )
Array
(
[0] => Array
(
[Artist] => Array
(
[id] => 1,
[name] => Wumpscut
),
[Album] => Array
(
[0] => Array
(
[id] => 1,
[artist_id] => 1,
[name] => Bunker Gate Seven
),
[1] => Array
(
[id] => 2,
[artist_id] => 1,
[name] => Born Again
)
),
[ArtistImage] => Array
(
[0] => Array
(
[id] => 1,
[artist_id] => 1,
[file] => Bunker-Gate-Seven-Cover.jpg
),
[1] => Array
(
[id] => 2,
[artist_id] => 1,
[file] => Born-Again-Cover.jpg
)
)
),
[1] => Array
(
[Artist] => Array
(
[id] => 2,
[name] => Oneriod Psychosis
),
[Album] => Array
(
[0] => Array
(
[id] => 3,
[artist_id] => 2,
[name] => Fantasies About Illness
),
),
[ArtistImage] => Array
(
[0] => Array
(
[id] => 3,
[artist_id] => 2,
[file] => Fantasies-About-Illness-Cover.jpg
),
)
)
)
// results
// with recursive 0
Array
(
[0] => Array
(
[Artist] => Array
(
[id] => 1,
[name] => Wumpscut
),
),
[1] => Array
(
[Artist] => Array
(
[id] => 2,
[name] => Oneriod Psychosis
)
)
)
// results
// with find( 'all', array( 'contain' => array( 'ArtistImage' ))
Array
(
[0] => Array
(
[Artist] => Array
(
[id] => 1,
[name] => Wumpscut
),
[ArtistImage] => Array
(
[0] => Array
(
[id] => 1,
[artist_id] => 1,
[file] => Bunker-Gate-Seven-Cover.jpg
),
[1] => Array
(
[id] => 2,
[artist_id] => 1,
[file] => Born-Again-Cover.jpg
)
)
),
[1] => Array
(
[Artist] => Array
(
[id] => 2,
[name] => Oneriod Psychosis
),
[ArtistImage] => Array
(
[0] => Array
(
[id] => 3,
[artist_id] => 2,
[file] => Fantasies-About-Illness-Cover.jpg
),
)
)
)
希望有助于解释它
答案 1 :(得分:2)
我认为问题在于你的艺术家模特。添加行为的正确方法是
<?php
Class Artist extends AppModel
{
public $name = "Artist";
public $actsAs = array( 'Containable' );
}
?>
注意actsAs变量是复数,你的模型将它作为$ actAs = array(..),变量为单数。
答案 2 :(得分:1)
您正在寻找可包含的行为(book.cakephp.org/view/474/Containable)。
递归属性(book.cakephp.org/view/439/recursive)告诉我们在获取记录时CakePHP应该在关系方面有多深。
答案 3 :(得分:0)
您可以将控制器和型号发布到pastebin中吗?你完成后在这里发布这些链接。
在您使用的pastebin条目中......
if( $images ) {
echo( "Loading Artist Images" );
$this->Artists->recursive = 1;
$this->Artists->contains( 'ArtistImage' );
} else { ...
由于一些原因,这不会起作用。
首先 - 模型名称必须是单数。 I.E.艺术家=艺术家 第二 - Containable行为没有函数“contains”,正确的函数名也是单数。 I.E包含('ArtistImage')= contains('ArtistImage')
所以我FTFY,你走了。
if( $images ) {
echo( "Loading Artist Images" );
$this->Artist->contain( 'ArtistImage' );
} else { ...
希望这能为您澄清一些事情。 http://book.cakephp.org/view/474/Containable http://book.cakephp.org/view/22/CakePHP-Conventions
此外,根据您使用的Cake版本,我相信调用不存在的模型函数(即Model-&gt; contains(...))将尝试从mysql调用存储过程文件没有该名称的功能。
答案 4 :(得分:-1)
模特是专辑,所以它应该是$ this-&gt;专辑而不是$ this-&gt;专辑
App :: import('Model','Album');
$这 - &GT;相册 - &GT;包含( '宋'); $ items = $ this-&gt;相册 - &gt; findAllByArtistId($ artist_id);