CakePHP通过关系读取hasMany
尝试显示 hasMany 表中的相关数据。它正在查找 joinModel 但未扩展到其他表的数据。
项目模型:
class Item extends AppModel {
public $name = 'Item';
public $belongsTo = array('Category');
public $hasMany = array('GroclistItem');
}
Groclist模型:
class Groclist extends AppModel {
public $name = 'Groclist';
public $belongsTo = array('Category');
public $hasMany = array('GroclistItem');
}
GroclistItem模型:
class GroclistItem extends AppModel {
public $name = 'GroclistItem';
public $belongsTo = array('Groclist', 'Item');
public $useTable = 'groclists_items';
}
GroclistController视图操作:
public function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid list', true));
$this->redirect(array('action' => 'index'));
}
$this->set('groclist', $this->Groclist->read(null, $id));
}
$groclist的调试:
app\View\Groclists\view.ctp (line 80)
Array
(
[Groclist] => Array
(
[id] => 3
[name] => This Week
[user_id] =>
[sunday] => Pizza
[monday] => Scallops
[tuesday] => Stuffed Mushrooms
[wednesday] => Mustard Chicken with Daulphanois Potatoes
[thursday] => Sizzling Chicken and Cheese
[friday] => Walkabout Soup with Beer Bread
[saturday] => Paradise Indian
[created] => 2011-09-15 14:42:55
[modified] => 2012-02-19 16:51:00
)
[GroclistItem] => Array
(
[0] => Array
(
[id] => 18
[item_id] => 24
[groclist_id] => 3
)
[1] => Array
(
[id] => 17
[item_id] => 23
[groclist_id] => 3
)
)
)
我希望 GroclistItem 数组显示项目数据,该数据应该是杂货清单项目,例如健怡可乐或肥皂。它在那里找到正确的关联。
Trying to display related data from a hasMany through table. It's finding the joinModel but not extending to the other table's data.
Item Model:
class Item extends AppModel {
public $name = 'Item';
public $belongsTo = array('Category');
public $hasMany = array('GroclistItem');
}
Groclist Model:
class Groclist extends AppModel {
public $name = 'Groclist';
public $belongsTo = array('Category');
public $hasMany = array('GroclistItem');
}
GroclistItem Model:
class GroclistItem extends AppModel {
public $name = 'GroclistItem';
public $belongsTo = array('Groclist', 'Item');
public $useTable = 'groclists_items';
}
GroclistController View Action:
public function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid list', true));
$this->redirect(array('action' => 'index'));
}
$this->set('groclist', $this->Groclist->read(null, $id));
}
Debug of $groclist:
app\View\Groclists\view.ctp (line 80)
Array
(
[Groclist] => Array
(
[id] => 3
[name] => This Week
[user_id] =>
[sunday] => Pizza
[monday] => Scallops
[tuesday] => Stuffed Mushrooms
[wednesday] => Mustard Chicken with Daulphanois Potatoes
[thursday] => Sizzling Chicken and Cheese
[friday] => Walkabout Soup with Beer Bread
[saturday] => Paradise Indian
[created] => 2011-09-15 14:42:55
[modified] => 2012-02-19 16:51:00
)
[GroclistItem] => Array
(
[0] => Array
(
[id] => 18
[item_id] => 24
[groclist_id] => 3
)
[1] => Array
(
[id] => 17
[item_id] => 23
[groclist_id] => 3
)
)
)
I would like the GroclistItem array to display the item data which should be a grocery list item such as Diet Coke or Soap. It's finding the correct associations in there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GroclistController 查看操作:
GroclistController View Action: