Phalcon is so beautifuI. Loving it a lot..
When I was trying to modify the invo application i came across a small problem. It may due to my ignorance. Anyway I will explain in detail.
I have 3 tables namely product_types, products, productDescription. Each tables contains a primary key. These three tables are logically connected, but no foreign key relations were defined.
Table product_types
id | name |
---|---|
1 | Vegetables |
2 | Fruits |
Table products
id | product_types_id | name |
---|---|---|
1 | 1 | Tomato |
2 | 1 | Beans |
3 | 2 | Apple |
4 | 2 | Mango |
Table productDescription
id | productid | description |
---|---|---|
1 | 1 | Green |
2 | 2 | Green |
3 | 3 | Red |
4 | 4 | Yellow |
Table hierarchy is product_types -> products -> productDescription
I am trying to achieve when I remove an entry from table product_types, I want the related entries from table products and productDescription has to be removed. i.e. If I delete Fruits from table product_types, products tables
entries having name Apple and Mango need to be deleted, plus entries from table productDescription have description Red and Yellow need to be deleted.
Model : ProductTypes.php
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Relation;
class ProductTypes extends Model
{
public $id;
public $name;
public function initialize()
{
$this->setSource("product_types");
$this->hasMany('id', 'Products', 'product_types_id', array( 'foreignKey' =>
array( 'action' => Relation::ACTION_CASCADE ) ));
}
}
Model: Products.php
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Relation;
class Products extends Model
{
public $id;
public $product_types_id;
public $name;
public function initialize()
{
$this->belongsTo('product_types_id', 'ProductTypes', 'id', array(
'reusable' => true
));
$this->hasOne('id', 'ProductDescription', 'productid', array('foreignKey' =>
array('action' => Relation::CASCADE)));
}
}
Model ProductDescription.php
use Phalcon\Mvc\Model;
class ProductDescription extends Model
{
public $id;
public $productid;
public $description;
public function initialize()
{
$this->setSource("productDescription");
$this->belongsTo('productid', 'Products', 'id', array(
'reusable' => true
));
}
}
But when I call
$productTypes->delete()
from the controller, its removing the entry from table product_types, but the corresponding entries from table products and productDescription were not removed, I dont know why but I have created virtual foreign keys and assigned action Relation::CASCADE
Thanking you very much