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

Form not showing related record

Hi All,

I am developing a simple ERP solution and have hit a roadbump... I have 2 models, one for products and one for Stock levels. The idea is that saving the product also saves the stock level. However, although when I create the product the stock level is saved correctly into the Stock table, when I try to edit a product, the field that should display the stock level comes up empty, although a value does exist.

This is the init for stock levels for the Products model:

$this->hasOne('id', __NAMESPACE__ . '\ProductStock', 'product_id', array(
    'alias' => 'ProductStock'
));

and this is the init for products for the Sotck model:

$this->hasOne('product_id', _NAMESPACE_ . '\Products', 'id', array(
    'alias' => 'Products'
));

They should have a 1-1 relationship between them. I know I am doing something wrong, I am just not sure what it is...

Thanks your your help...

$this->hasOne('product_id', _NAMESPACE_ . '\Products', 'id', array(
    'alias' => 'Products'
));

It should be belongsTo, not hasOne

Tried that too, did not work however... any ideas?



11.6k
Accepted
answer

if the problem is in the form input, show how you have assigned the value to it please... you should have something like

       public function editAction($id) 
       {
         $model = Model::findFirstByid($id);
        ...
        $this->tag->setDefault("myField", $model->relatedAlias->column);
        ...
    }

Tried that too, did not work however... any ideas?

So there is basically no related data ? Show your whole classes and your database records for those results maybe ? It should work, if it's not returned without form then there is something wrong.