We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Failed to delete records from related tables

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



1.1k
Accepted
answer

Seems I have sorted out my problems:

PROBLEM 1#

A Typo in my Model -> Products.php

Error Code:

$this->hasOne('id', 'ProductDescription', 'productid', array('foreignKey' => 
                 array('action' => Relation::CASCADE)));

Instead of Relation::ACTION_CASCADE I have written Relation::CASCADE.

Corrected now:

$this->hasOne('id', 'ProductDescription', 'productid', array('foreignKey' => 
                 array('action' => Relation::ACTION_CASCADE)));

PROBLEM 2#

In my scenario table products and table productDescription are in 1-1 relationship.

But in Model: ProductDescription.php I have assigned relationship belongsTo towards table products.

As a result this will not remove matched row from table productDescription.

Now I changed the relationship to hasOne.

Great it is working again.

My Modified code of Model ProductDescription.php .

    public function initialize()
        {
                $this->setSource("productDescription");
                $this->hasOne('productid', 'Products', 'id');
        }

Tnx

Phalcon is Amazing!!!