PDA

View Full Version : ویرای محصولات با چند جدول



Mahdi-563
دوشنبه 19 فروردین 1392, 14:34 عصر
سلام دوستان

من یک مدل دارم که چند نوع رابطه چند به یک، یک به یک و ... داره حال اینکه من توی ایجاد و حدف مشکلی ندارم ولی توی آپدیت با متد saveAssociated مشکل دارم

جالب اینکه با متد save فقط روابط چند به یک رو ذخیره می کنه ولی یک به یک را ذخیره نمی کنه

ممنون

اینم مدلم

public $hasOne = array(
'Description' => array(
'className' => 'Description',
'dependent' => true,
'unique' => true,
),
'Tag' => array(
'className' => 'Tag',
'dependent' => true,
'unique' => true,
),
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Language' => array(
'className' => 'Language',
'foreignKey' => 'language_id',
'conditions' => '',
'fields' => '',
'order' => '',
),
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'ProductImage' => array(
'className' => 'ProductImage',
'foreignKey' => 'product_id',
'conditions' => '',
'fields' => '',
'order' => 'sort_order DESC',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
, 'dependent' => true),
'ProductOption' => array(
'className' => 'ProductOption',
'foreignKey' => 'product_id',
'conditions' => '',
'fields' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
, 'dependent' => true
),
'ProductSpecial' => array(
'className' => 'ProductSpecial',
'foreignKey' => 'product_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Relate' => array(
'className' => 'Product',
'joinTable' => 'products_relate',
'foreignKey' => 'product_id',
'associationForeignKey' => 'relate_id',
// 'unique' => 'keepExisting',
'dependent' => true,
),
'productStores' => array(
'className' => 'productStores',
'joinTable' => 'product_stores',
'foreignKey' => 'product_id',
'associationForeignKey' => 'store_id',
// 'unique' => 'keepExisting',
'dependent' => true
),
'attribute' => array(
'className' => 'attribute',
'joinTable' => 'product_attributes',
'foreignKey' => 'product_id',
'associationForeignKey' => 'attribute_id',
// 'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => 'sort_order DESC',
'dependent' => true
),
'Category' => array(
'className' => 'Category',
'joinTable' => 'product_categories',
'foreignKey' => 'product_id',
'associationForeignKey' => 'category_id',
// 'unique' => 'keepExisting',
'conditions' => '',
'fields' => 'name',
'dependent' => true
),
);

zoghal
سه شنبه 20 فروردین 1392, 00:59 صبح
درورد بر شما
الظاهر من این مشکلات رو دارم می بینیم تو تعریف ها تون.
1- در رابطه hasOne ما اصلا unique نداریم.. foreignKey رو مشخص نکردید.
2- در رابطه hasAndBelongsToMany زیر



'Category' => array(
'className' => 'Category',
'joinTable' => 'product_categories',
'foreignKey' => 'product_id',
'associationForeignKey' => 'category_id',
// 'unique' => 'keepExisting',
'conditions' => '',
'fields' => 'name',
'dependent' => true
),

مقدار فیلد رو محدود به name کردید. باید کلید ها هم باشه.

درضمن برای ذخیره این رابطه ای شما باید. از saveAll استفاده کنید.

باز اگر مشکلی داشتید . قبل از ذخیره کردن. از دستور زیر استفاده کنید و خروجی رو بزارید تا بشه بهتر دیباگ کرد مشکلتون رو


pr($this->request->data);