php - 使用 Cakephp,如何在我的脚手架添加、编辑 View 中获取多选元素或 hasMany 关系的复选框?

标签 php cakephp has-many scaffolding

我已经浏览了有关将模型链接在一起(创建关联)的 Cakephp 文档,并且通过在模型中定义 $hasOne 成员一个模型和相应的 $belongsTo ,我可以在脚手架添加和编辑 View 中获得单个选择下拉列表其他。现在,对于 $hasMany 关系,我试图在我的脚手架添加和编辑 View 中获取某种表单输入(多选下拉列表或复选框......最好是复选框),让用户选择关联的广告对于给定的租金。但是使用下面的代码,我在租赁添加和编辑 View 中没有提到广告:'( 我错过了什么吗?这可能吗?我已经在 RoR 和 Grails 中看到了这一点,但无法让它与 CakePHP 一起工作.感谢您的帮助!

租赁模型(app/Model/Rental.php)

<?php
class Rental extends AppModel {
    var $name = 'Rental';             
    var $belongsTo = array(
        'Agent' => array(
            'className'    => 'User',
            'foreignKey'    => 'agent_id'
        ),
        'Bedroom',
        'Landlord' => array(
            'className'    => 'Landlord',
            'foreignKey'    => 'landlord_id'
        )
    );  
    var $hasMany = array(
        'Advertisement' => array(
            'className'     => 'Advertisement',
            'foreignKey'    => 'rental_id',
            //'conditions'    => array('Comment.status' => '1'),
            'order'    => 'Advertisement.created DESC',
            'limit'        => '5',
            'dependent'=> false
        )
    );

    public $validate = array(
        'title' => array(
            'rule' => 'notEmpty'
        ),
        'description' => array(
            'rule' => 'notEmpty'
        )
    );

    public function isOwnedBy($rental, $user) {
        return $this->field('id', array('id' => $rental, 'user_id' => $user)) === $rental;
    }
}

租赁 Controller (app/Controller/RentalsController.php)

<?php
class RentalsController extends AppController {
    public $scaffold;
}

广告模型(app/Model/Advertisement.php)

<?php
class Advertisement extends AppModel {
    var $name = 'Advertisement';                   
    var $belongsTo = array(
        'Rental' => array(
            'className'    => 'Rental',
            'foreignKey'    => 'rental_id'
        ),
        'Author' => array(
            'className'    => 'User',
            'foreignKey'    => 'author_id'
        )
    ); 
}

广告 Controller (app/Controller/AdvertiesementsController.php)

<?php
class AdvertisementsController extends AppController {
    public $scaffold;
}

最佳答案

Cake 的脚手架 View 不显示关系的 hasMany 方面的任何内容。他们假设您想从具有“belongsTo”的模型中选择它。

坦白说,对于这种关系来说,将其作为多项选择有点奇怪,恕我直言。多选通常用于 hasAndBelongsToMany 关系。如果你想做的话,你需要自己做,而不是使用 Cake 的脚手架。

关于php - 使用 Cakephp,如何在我的脚手架添加、编辑 View 中获取多选元素或 hasMany 关系的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13654290/

相关文章:

mysql - rails Mysql : How to only include has_many relation object that meet my condition?

CakePHP 1.3 在添加新帖子后清除所有缓存页面

cakephp - 如何处理以HTTP Post形式发送到cakephp应用程序的json数据?

php - Yii2中如何实现mysql记录锁定

PHP/MySQL 查询未执行

cakephp - 在 CakePHP 中定义模型类的层次结构

json - 使用 sequelize 防止将连接表数据添加到 json

cakephp - hasMany 条件显示不需要的记录 cakephp 1.3

PHP 数据访问设计模式以补充 ORM

php - 如何防止欺骗性发布到 PHP 喜欢/不喜欢的系统