2014-04-27 59 views
1

我正在尝试在产品表(产品)和名为brick_types的BrickTypes表之间创建belongsTo关系。我的型号如下:无法保存模型CakePHP属于

class Product extends AppModel { 
    public $belongsTo = array('BrickType'); 

    public $hasAndBelongsToMany = array(
    'colour_categories' => 
     array(
      'joinTable' => 'colours_products', 
      'foreignKey' => 'product_id', 
      'associationForeignKey' => 'category_id', 
      'with' => 'colours_products' 
     ) 
    ); 
    } 

我BrickType模型如下:

class BrickType extends AppModel { 
public $name = 'BrickType'; 
    public $useTable = 'brick_types'; 
    public $displayField = 'type'; 
    public $hasMany = array(
    'Product' => 
     array(
      'className' => 'Product', 
      'foreignKey' => 'brick_type_id' 
     ) 
    ); 
} 

我brick_types表如下所示:

enter image description here

我的产品表如下所示:

enter image description here

有没有人有任何想法我可以解决这个问题?我的模型不会保存。我已经尝试saveAssociated和saveAll但没有成功。

谢谢!

+0

“colours_products”是一张表吗? – s4suryapal

+0

该表是一种将颜色链接到产品的HABTM。 –

回答

1

您的问题尚不清楚,我仍试图通过一个例子给出答案: 假设有两种模型,分别是:“Product”和“BrickType”。 你想要做的这两款车型之间的连接,比代码应该是这样的:

对于型号BrickType:

class BrickType extends AppModel { 

     public $name  = 'BrickType'; 
     public $useTable = 'brick_types'; 
     public $primaryKey = 'id'; 
     public $belongsTo = array(
      'Product' => array(
       'className' => 'Product', 
       'foreignKey' => 'brick_type_id', 
       'dependent' => 'true' 
      ) 
     ); 
} 

对于型号产品:

class Product extends AppModel { 

     public $name   = 'Product'; 
     public $useTable  = 'products'; 
     public $primaryKey = 'id'; 
     public $displayField = 'name'; 


} 

对于产品控制器:

class ProductsController extends AppController { 
    public $uses = array('Product','BrickType'); 
    $products = $this->Product->find('all'); 
    echo "<pre>";print_r($products);exit; 
} 
+0

我的模型现在没有在信息之前填充信息。这里是填充砖型的控制代码: \t'$ this-> loadModel('BrickType'); \t \t $ brick_types = $ this-> BrickType-> find('list'); \t \t \t \t // echo“

" . print_r($brick_types, true) . "
”; \t \t // $ product = $ this-> BrickType-> findById($ id); \t \t // echo“
" . print_r($this->request, true) . "
”;我不知道我有一个困难的时间格式化代码,不完全得到stackoverflow方向。 –

+0

任何想法的人? –

+0

我已经把产品控制器的代码,我希望它能正常工作 – s4suryapal