XF 2.2 Relation with another add-on

FoxSecrets

Active member
I am trying to build a relation from addon1 to addon2 (one to one), but it's not working. Something missing on my configuration?
Some questions:
  • On the example below, what does 'Category' relation refers to, can be any name?
  • Is my 'entity' correct to point to another addon?
  • Does the recording order matter?

ADDON 1
Code:
// ...
    $structure->columns = [
      'product_id'   => ['type' => self::UINT, 'autoIncrement' => true],
      'product'        => ['type' => self::STR, 'maxLength' => 50, 'default' => '']
      'category_id'  => ['type' => self::UINT, 'default' => 0],
    ];
    $structure->relations = [
      'Category' => [
        'entity'        => 'My\Addon2:Class',
        'type'          => self::TO_ONE,
        'conditions'    => 'category_id',
        'primary'       => true
    ],
    // ...


ADDON 2 - My\Addon2:Class
Code:
// ...
    $structure->columns = [
      'category_id'   => ['type' => self::UINT, 'autoIncrement' => true],
      'category'      => ['type' => self::STR, 'maxLength' => 50, 'default' => '']
    ];
    // ...
 
Last edited:
I am trying to build a relation from addon1 to addon2 (one to one), but it's not working.
What do you mean when you say it's not working?

On the example below, what does 'Category' relation refers to, can be any name?
Yeah, it is the name of the relation and can be anything of your choosing. It does not necessarily need to match the name of the entity. You access the relation using it ($entity->Category will give you the relation entity).

Is my 'entity' correct to point to another addon?
It looks fine to me. Is your My\AddOn2:Class entity in src/addons/My/AddOn2/Entity/Class.php? And does it have its structure set correctly (table, shortName, primaryKey etc.)?

Does the recording order matter?
The ordering does not matter.
 
What do you mean when you say it's not working?
Whatever new record I do, there is no reference (always default zero).
It looks fine to me. Is your My\AddOn2:Class entity in src/addons/My/AddOn2/Entity/Class.php? And does it have its structure set correctly (table, shortName, primaryKey etc.)?
Both structures are correctly set. Both work independently but the relation not.
I have no idea what's going on then :unsure:
 
Last edited:
Whatever new record I do, there is no reference (always default zero).
How are you accessing the reference? The value for a relation that can't be found is null and not 0. If your category_id is 0 then there would be no relation, so ensure it is set to a valid (existing) category_id. If you open the debug page (click the timing information at the bottom right) on a page which tries to access it, it will show you the query it's running.
 
Well I checked the reference directly on the table after submitting the data and it's zero because I've set default value 'category_id' => ['type' => self::UINT, 'default' => 0],. I also checked the debug page as you said, but I haven't found the queries. :unsure: But the data is being recorded in both tables at the time of submit action. Is this ok? Or should "Category" table have data before submission?
 
Well I checked the reference directly on the table after submitting the data and it's zero because I've set default value 'category_id' => ['type' => self::UINT, 'default' => 0],.
Oh, XF does not set the value for you unless the child entity is instantiated by calling \XF\Mvc\Entity\Entity::getDefaultRelation on the parent entity. Otherwise you can insert the parent entity first and set it before inserting the child entity.

Using \XF\Mvc\Entity\Entity::getDefaultRelation (assumes you have a reverse Product relation set up):
PHP:
$category = $this->em->create('My\AddOn2:Category');
// set category values

$product = $category->getDefaultRelation('Product');
// set product values...

$category->save(); // will cascade-save the product for you

Inserting one at a time:
PHP:
$category = $this->em->create('My\AddOn2:Category');
// set category values
$category->save();

$product = $this->em->create('My\AddOn1:Product');
$product->category_id = $category->category_id;
// set product values
$product->save();
 
Inserting one at a time:
PHP:
$category = $this->em->create('My\AddOn2:Category');
// set category values
$category->save();

$product = $this->em->create('My\AddOn1:Product');
$product->category_id = $category->category_id;
// set product values
$product->save();
I followed this option and got it working now! So basically it's necessary to record the id back to the other add-on, didn't know that.
Thank you @Jeremy P !
 
Top Bottom