Skip to content
Advertisement

Getting the Linked Model Correctly in the Cycle ORM

There was a problem: The project uses Cycle ORM, there are 2 Entities. I am trying to link them using annotation like so:

/**
* @CycleRelationBelongsTo(target = "CompanyCoreEntityCourtInfoCourtInfo")
*/
protected $court;

But when accessing $repository->findOne()->getCourt() I get:

object(CycleORMPromiseReference)#2212 (2) {
["role":"CycleORMPromiseReference":private]=>
string(42) "CompanyCoreEntityCourtInfoCourtInfo"
["scope":"CycleORMPromiseReference":private]=>
array(1) {
["id"]=>
int(15705)
 }
}

But I want to get Entity. How can I do this correctly? The option works if you add the annotations fetch = “eager” to the parameters, but then it will be loaded eagerly. How can I lazily load related entities?

Advertisement

Answer

protected $court is your relation or link as you would like to refer to it as.

Here is a similar piece of code of a fork of Yii3 demo that uses Cycle from a working example which you can download here:

 /**
 * @BelongsTo(target="Family", nullable=false)
 *
 * @var CycleORMPromiseReference|Family
 */
private $family = null;

The file that it belongs to is Product.php. This file represents the entity Product. This file also contains the following code.

/**
* @Column(type="integer(11)", nullable=true, default=null)
*/
private ?int $family_id = null;

What is a family? The family is the group that the product falls under eg. Shoes.

How does Cycle use these annotations and what is the difference between the two? If you have synchronized your database here

…then if you delete the whole Product table in say mySql, Cycle will rebuild the Product table and its table’s foreign key ie. family_id for the Product table by referencing the Family entity’s primary key ie. $id which is listed in the Family.php entity file and append this to your relation $family to create the foreign key family_id in the Product table. ie. $family + $id => family_id. All the necessary indexes and foreign key constraints will be created in mySql automatically.

So you have to be careful how you call your primary key in Family.php entity file or you could end up with a foreign key in your Product table such as family_family_id if you named your primary key in Family.php entity as $family_id.

So if Product tables’s family_id foreign key is created automatically by the entities $family relation why have the second piece of code in your Product entity. ie. $family_id = null ? This is so that Cycle can use eg. nullable = true , default = null to automatically build mySql properties for the column ie. YES, NULL.

You will need to ensure:

  1. That your entity file uses a use statement eg.
use AppInvoiceEntityFamily  

that points to the Entity eg. Family

  1. That you have a reference before the annotation if you are using a framework eg.

    * @var CycleORMPromiseReference|Family
    

How does the $family get used in a repository? eg. EntityProductProductRepository

public function repoProductquery(string $product_id): Product
    {
        $query = $this
            ->select()
            ->load('family')
            ->load('tax_rate')
            ->load('unit')
            ->where(['id' => $product_id]);
        return  $query->fetchOne();        
    }

In the above code the ‘family’ relation is being loaded and will enable the product view to use code

<?= $product->getFamily()->family_name;?>

to retrieve the family_name from the Family entity.

Points: $product is passed from the ProductController here:

…using

//load EntityProduct BelongTo relations ie. $family, $tax_rate, $unit by means of repoProductQuery             
            'product'=>$productRepository->repoProductquery($this->product($request, $productRepository)->getProduct_id()),

and getFamily() is a function in the Product Entity similar to your getCourt() with importantly this piece of code on line 134 that retrieves the relation …

//relation $family
   public function getFamily(): ?Family
   {
        return $this->family;
   }

Notice that the above getFamily() function has the ?Family type which points the Product Entity to the Family Entity.

How can I lazily load related entities?

/**
     * @BelongsTo(target="AppUserUser", nullable=false, load="eager")
     *
     * @var CycleORMPromiseReference|User
     */
    private $user = null;

Answer: Remove load=”eager” or load=”null”.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement