Call to a member function add() on a non-object

Hi  All,

                 I am having a custom module con_ProFormas

I have created a one to many relation ship to documents.

I want to attac a document to this custom  module

In my code i am using the following but getting an error.

$docbean = BeanFactory::newBean("Documents");
$docbean->name = 'Record';
$docbean->save();


$rel_name="documents";
$cp = new con_ProFormas();
$cp->retrieve($this->bean->id);
$cp->load_relationship($rel_name);
$cp->$rel_name->add($docbean->id);
$cp->save();
   

But i am getting an error like fatal Call to a member function add() on a non-object

Can any one please guide me where i am wrong.

Regards

SIdhu André Lopes Francesca Shiekh hats

  • Hi sidhu sidhu,

    Are you sure your relationship link field name is "documents" ? If not sure, please navigate to record detail view of con_ProFormas module and do inspect element on Documents subpanel, it will show the link name which we need to use while loading relationship using bean.

    Inspect Element Screenshot

    Also, as a best practice, always use if condition for load_relationship to avoid error during run-time.

    if($cp->load_relationship($rel_name)) { //your logic goes here }

    Regards.

    Hats

  • Hi hats,

                Checked it in developer tool.Thank you .

    It is working.

    $rel_name="con_proformas_documents_1";//this is now correct
    $cp = new con_ProFormas();
    $cp->retrieve($this->bean->id);
    if($cp->load_relationship($rel_name))
    {
      $cp->$rel_name->add($docbean->id);
      $cp->save();
    }


    Regards

    Sidhu