I am stuck on creating / updating relations of a model.
So the models I have at this moment are Product
model, ProductAttribute
model and a Attribute
model.
Here is the code for all of them
Product
<?php
namespace Greatmedia\Models\V1;
class Product extends \Phalcon\Mvc\Model
{
public $id;
public $name;
public $price;
public function getSource()
{
return 'product';
}
public function initialize()
{
// $this->hasMany('id', '\Greatmedia\Models\V1\ProductAttribute', 'product_id', [
// 'alias' => 'attributes'
// ]);
$this->hasManyToMany(
'id',
'\Greatmedia\Models\V1\ProductAttribute',
'product_id',
'attribute_id',
'\Greatmedia\Models\V1\Attribute',
'id',
['alias' => 'attributes']
);
}
}
ProductAttribute
<?php
namespace Greatmedia\Models\V1;
class ProductAttribute extends \Phalcon\Mvc\Model
{
public $id;
public $product_id;
public function getSource()
{
return 'product_attribute';
}
public function initialize()
{
$this->belongsTo('product_id', '\Greatmedia\Models\V1\Product', 'id', [
'alias' => 'product'
]);
$this->belongsTo('attribute_id', '\Greatmedia\Models\V1\Attribute', 'id', [
'alias' => 'attribute'
]);
}
}
Attribute
<?php
namespace Greatmedia\Models\V1;
class Attribute extends \Phalcon\Mvc\Model
{
public $id;
public $value;
public $label;
public function getSource()
{
return 'attribute';
}
public function initialize()
{
$this->hasMany('id', '\Greatmedia\Models\V1\ProductAttribute', 'attribute_id', [
'alias' => 'product_attribute'
]);
}
}
Nothing really fancy, just as the docs told me to do.
What I am doing is a CLI, now that it self works. Creating and updating products is no issue.
I just can't seem to a)create and b)update attributes.
Here is my ImportTask
.
ImportTask
<?php
use Greatmedia\Models\V1\Product;
use Greatmedia\Importer\Repository;
use Phalcon\Mvc\Model;
use Greatmedia\Models\V1\Attribute;
class ImportTask extends \Phalcon\CLI\Task
{
public function mainAction(array $params = []) {
if (!empty($params)) {
$this->importOne();
}
else {
$this->importAll();
}
}
public function importAll()
{
$repo = new Repository();
$repo->attachAll();
while($repo->valid()) {
$importer = $repo->current();
foreach ($importer->getProducts() as $prod) {
$prod_data = $prod['product'];
$attributes = $prod['attributes'];
$sku = $prod_data['sku'];
$product = $this->getBySkuOrNew($sku);
$this->createAttributes($product, $attributes);
if ($product->save($prod_data) === false) {
echo 'Unable to create product.' . PHP_EOL;
$this->printMessages($product);
continue;
}
echo 'Product stored: ' . $product->id . PHP_EOL;
}
$repo->next();
}
}
public function createAttributes(Model $product, array $attributes_arr)
{
foreach ($product->getAttributes() as $attribute) {
foreach ($attributes_arr as $attr) {
foreach ($attr as $attr_key => $attr_val) {
$attribute->{$attr_key} = $attr_val;
}
if ($attribute->save() === false) {
echo 'Unable to create / update attribute.';
$this->printMessages($attribute);
continue;
}
echo 'Attribute created / updated for product: ' . $product->id;
}
}
}
public function printMessages(Model $model)
{
foreach ($model->getMessages() as $message) {
echo $message . PHP_EOL;
}
}
public function getBySku($sku)
{
return Product::findFirst("sku = '{$sku}'");
}
public function getBySkuOrNew($sku)
{
$product = $this->getBySku($sku);
if ($product === false) {
$product = new Product();
}
return $product;
}
}
If at least there was some error or something, but there is,,, nothing. The products get stored, the product stored message is echo'd and that's it.