【问题标题】:CakePHP 3.6.11: Access property of joined tables in viewCakePHP 3.6.11:视图中连接表的访问属性
【发布时间】:2018-10-05 19:58:00
【问题描述】:

我有这张桌子:

客户[id, name, surname, phone, text, balance, created]

service_types[id, title, price, length, is_subscription, created]

customer_service_types[id, customer_id, service_type_id, price, created]

view.ctp 中,我想查看分配给当前客户的所有Services

BakeCustomersController.php 中创建了这个view function

public function view($id = null)
    {
        $customer = $this->Customers->get($id, [
            'contain' => ['CustomerServiceTypes']
        ]);

        $this->set('customer', $customer);
    }

view.ctp 中,我想显示service_type -> title,所以我尝试这样做:

<td><?=$customerServiceTypes->service!==null ? h($customerServiceTypes->service->title) : '' ?></td>

但它总是显示空白。我怎样才能改变它才能工作?

【问题讨论】:

    标签: php join cakephp model-view-controller associations


    【解决方案1】:

    改变视图功能如下:

    $customer = $this->Customers->get($id, [
                'contain' => ['CustomerServiceTypes' => ['ServiceTypes']]
            ]);
    
    
            $this->set('customer', $customer);
    

    在view.ctp中:

    <td><?= h($customerServiceTypes->service_type->title) ?></td>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 2013-09-18
      相关资源
      最近更新 更多