Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:34:50+00:00 2026-06-18T04:34:50+00:00

In my project i have entities table, where all entities should exist (to support

  • 0

In my project i have entities table, where all entities should exist (to support complex foreign keys), so i need to insert additional row in special table (in this entities list) before inserting a row to my model table, and i wanted to ask what is the best way to do this.

So, for following code i need to insert two rows: in entity table, then pick up id for just inserted row, save it in current model and insert in accounts table:

$account = new Account();
$account->name = 'John';
$account->save(); // performs two inserts while creating model

As i understood, i can use beforeCreate() method to insert row in entity table and to pick up id for newly created row, smth like this:

class Account
{
    public function beforeSave()
    {
        $entity = new \Entity();
        $entity->type = get_class($this);
        $entity->save();
        $this->id = $entity->id;
    }
}

But this way, if somehow account row will be not inserted, row in entity table will exist.

Then i thought to use transactions as shown in docs here http://docs.phalconphp.com/en/latest/reference/models.html#transactions

But i don’t unserstand, if i have small transactions for every model::create() method, how it will work when i will need transactions for complex operations?

e.g.

// controller action context
use Phalcon\Mvc\Model\Transaction\Manager as TxManager,
    Phalcon\Mvc\Model\Transaction\Failed as TxFailed;

try {

    //Create a transaction manager
    $manager = new TxManager();

    // Request a transaction
    $transaction = $manager->get();

    $account = new Account();
    $account->setTransaction($transaction);
    $account->name = "WALL·E";
    $account->created_at = date("Y-m-d");
    if ($account->save() == false) { // sub-transaction inside account::create() method
        $transaction->rollback("Cannot save account");
    }

    $accountPart = new AccountPart();
    $accountPart->setTransaction($transaction);
    $accountPart->type = "head";
    if ($accountPart->save() == false) { // sub-transaction inside accountPart::create() method
        $transaction->rollback("Cannot save account part");
    }

    //Everything goes fine, let's commit the transaction
    $transaction->commit();

} catch(TxFailed $e) {
    echo "Failed, reason: ", $e->getMessage();
}

It’s hard to imagine how it will work in big project.. nested transactions it’s not very good for database perfomance

Also i thought about 3d method of implementation, i’ve added it’s code below, but it looks like a hack and i also don’t want to use it:

public function create($data = null)
{   
    // Create abstract entity instance
    $entity = new \Entity();
    $entity->type = get_class($this);

    // Save abstract entity
    if (!$entity->save()) {
        return false;
    }

    // Save current entity
    $this->id = $entity->id;
    $result = parent::create($data);

    // Remove abstract entity if current row was not saved
    if (!$result) {
        $entity->delete();
    }

    return $result;
}

What is the best and easy way to support such complex entities?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:34:51+00:00Added an answer on June 18, 2026 at 4:34 am

    The easiest way to implement transactions is using 0.9.0:

    class Account
    {
        public function beforeCreate()
        {
            $entity = new \Entity();
            $entity->type = get_class($this);
            $this->entity = $entity;
        }
    
        public function initialize()
        {
            $this->belongsTo(array('entity_id', 'Entity', 'id'));
        }
    
    }
    

    On the other hand, the transaction manager creates an isolated connection that allow you to query the records modified in the current transaction snapshot, as well see the not isolated records.

    Here, there is an explanation of different transaction scenarios in the new docs: http://docs.phalconphp.com/en/0.9.0/reference/models.html#transactions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a project using FluentNibernate to map the entities. Now I need to
I have a project with 3 artifacts: common - entities, business logic. no UI
In a project that I am working on, I have the following entities: Analyst
I have entity Product and entity Subcategory : Subcategory.php namespace Project\Entities; /** * Subcategory
For example: I have two entities named Project and Todo where a project has
I have two entities: ResourceFile: @Entity @Table(name = resource_file) public class ResourceFile extends IdEntity<Integer>
In my project, I have two Core Data entities. One is the grouping (can
I'm working on a JPA project in which I have some different Entities extending
I have the following entities: EMPLOYEE (id, fname, lname, ...) PROJECT (id, ...) EMPLOYEE_PROJECT
I need to create entities (specifically contracts) in a database table that are associated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.