CakePHP通过关系读取hasMany

发布于 2025-01-06 19:06:55 字数 2106 浏览 0 评论 0原文

尝试显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2025-01-13 19:06:55

GroclistController 查看操作:

public function view($id = null) {
if (!$id) {
    $this->Session->setFlash(__('Invalid list', true));
    $this->redirect(array('action' => 'index'));
}
$this->Groclist->recursive =2;
$this->set('groclist', $this->Groclist->read(null, $id));
}

GroclistController View Action:

public function view($id = null) {
if (!$id) {
    $this->Session->setFlash(__('Invalid list', true));
    $this->redirect(array('action' => 'index'));
}
$this->Groclist->recursive =2;
$this->set('groclist', $this->Groclist->read(null, $id));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文