dq8081 2014-05-02 20:49
浏览 29

Cakephp Saveassociated Deep => true数组格式问题

So, I have a project with the following relationships:

In Listing.php
public $belongsTo = array(
        'User' => array(
            'foreignKey' => 'user_id'));
In User.php
public $hasMany = array(
            'Referral' => array(
                'foreignKey' => 'user_id',
                'dependent' => true),
            'Listing' => array(
                'foreignKey' => 'user_id',
                'dependent' => true),
            'Appointment' => array(
                'foreignKey' => 'user_id',
                'dependent' => true
            ));

Basically, when I create a new Listing, I would also like to create a Referral and an Appointment. I have been naming my form fields like so:

name="data[User][Referral][name]", and I also tried name="data[Referral][name]"

and then tried using:

if ($this->request->data['User']['Referral']) {
    $this->request->data['User']['id'] = $this->Auth->user('id');
}

$this->request->data['Listing']['user_id'] = $this->Auth->user('id');
debug($this->request->data);    
if($this->Listing->SaveAssociated($this->request->data, array('deep' => true))) {

In the case of the former field naming convention, everything fails, and my output looks like:

EDIT: Everything was failing due to a validation error. On success, I get the same result as with the latter naming convention - the fields are created (N of them where N is the number of fields passed in the model, ie 2 for appointment, 4 for Referral) and all their values are blank.

array(
'Listing' => array(
    'sale_type' => '',
    'property_type' => '',
    'referer' => '',
    'price' => '',
    'access_method' => '',
    'bedrooms' => '',
    'bathrooms' => '',
    'ext_stories' => '',
    'levels' => '',
    'sq_feet' => '',
    'sq_feet_src' => '',
    'builder' => '',
    'year_built' => '',
    'guest_sq_feet' => '',
    'user_id' => '2'
),
'User' => array(
    'Appointment' => array(
        'appointment_date' => '',
        'appointment_date2' => ''
    ),
    'Referral' => array(
        'name' => 'katie',
        'contact' => '',
        'relationship' => ''
    ),
    'id' => '2'
)

I can tell this is not quite the way the data is formatted in the book, but I can't figure out how to name the fields to get it that way.

If I use the latter naming convention, I get this as output:

array(
'Listing' => array(
    'sale_type' => '',
    'property_type' => '',
    'referer' => '',
    'price' => '',
    'access_method' => '',
    'bedrooms' => '',
    'bathrooms' => '',
    'ext_stories' => '',
    'levels' => '',
    'sq_feet' => '',
    'sq_feet_src' => '',
    'builder' => '',
    'year_built' => '',
    'guest_sq_feet' => '',
    'user_id' => '2'
),
'Appointment' => array(
    'appointment_date' => '',
    'appointment_date2' => ''
),
'Referral' => array(
    'name' => 'katie',
    'contact' => '',
    'relationship' => '',
    'user_id' => '2'
)

This will at least save, but I will end up with 4 Referral records (one for each line of data supplied) that have NULL for name, contact and relationship.

This is my first time using SaveAssociated deep = true, and I'm not sure what I'm not doing right. I'm assuming I should be going with the former approach (nesting these Models under User), but I'm not sure how to name the fields to get the data to look more like the example supplied here

SOLVED I am an idiot, and finally got it:

name="data[User][Referral][0][name]"
  • 写回答

1条回答 默认 最新

  • dongre8505 2014-05-03 14:48
    关注

    I am an idiot, and finally got it:

    name="data[User][Referral][0][name]"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c