Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Storing related records: Object of class Multiple\Frontend\Models\Attributes could not be converted to string

Hello,

I'm trying to run a example in the Storing related records for 3 table but all I get is an error message reading Object of class Attributes could not be converted to string.

--
-- Table structure for table `entity_main`
--

CREATE TABLE `entity_main` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_persian_ci NOT NULL,
  `origin` int(11) NOT NULL,
  `destination` int(11) NOT NULL,
  `date_start` int(11) NOT NULL,
  `date_end` int(11) NOT NULL,
  `description` text COLLATE utf8_persian_ci NOT NULL,
  `agency_id` int(11) NOT NULL,
  `los` varchar(255) COLLATE utf8_persian_ci NOT NULL,
  `min_price` int(11) NOT NULL,
  `discount_percent` int(11) NOT NULL,
  `discount_price` int(11) NOT NULL,
  `date` int(11) NOT NULL,
  `registrar` int(11) NOT NULL,
  `city_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_entity_tours_city1_idx` (`city_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=41 ;

--
-- Table structure for table `attributes`
--

CREATE TABLE `attributes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_persian_ci NOT NULL,
  `entity_kind` varchar(255) COLLATE utf8_persian_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=2 ;

--
-- Table structure for table `attribute_values`
--

CREATE TABLE `attribute_values` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `value` varchar(255) COLLATE utf8_persian_ci NOT NULL,
  `attribute_id` int(11) NOT NULL,
  `entity_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_attribute_values_attributes_idx` (`attribute_id`),
  KEY `fk_attribute_values_entity_tours1_idx` (`entity_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=11 ;


// model 1
namespace Multiple\Frontend\Models;

use Phalcon\Mvc\Model;

class Entity extends Model
{

    public $id;

    public $title;

    public $origin;

    public $destination;

    public $date_start;

    public $date_end;

    public $description;

    public $agency_id;

    public $los;

    public $min_price;

    public $date;

    public $registrar;

    public $city_id;

    public $discount_price;


    public function initialize()
    {
        $this->setSource("entity_tour");
        $this->hasMany("id", "Attribute_values", "entity_id");
    }

    public function getSource()
    {
        return "entity_main";
    }

}

// model 2
namespace Multiple\Frontend\Models;

use Phalcon\Mvc\Model;

class Attributes extends Model
{

    public $id;

    public $title;

    public $entity_kind;

    public function initialize()
    {
        $this->hasMany("id", "Attribute_values", "attribute_id");
    }

}

//model 3
namespace Multiple\Frontend\Models;

use Phalcon\Mvc\Model;

class Attribute_values extends Model
{

    public $id;

    public $value;

    public $attribute_id;

    public $entity_id;

    public function initialize()
    {
        $this->belongsTo("entity_id", "Entity", "id");
        $this->belongsTo("attribute_id", "Attributes", "id");
    }

}

// action and run this
public function createAction()
    {

        // Create an Entity 
        $entity = new models\Entity();
        $entity->title = 'test title';
        $entity->city_id = '2';
        $entity->origin = '2';
        $entity->destination = '2';
        $entity->date_start = '2';
        $entity->date_end = '2';
        $entity->description = '2';
        $entity->destination = '2';
        $entity->agency_id = '2';
        $entity->los = '2';
        $entity->min_price = '2';
        $entity->date = '2';
        $entity->discount_price = '2';
        $entity->discount_percent = '2';
        $entity->registrar = '2';

        // Create an attribute
        $attributes = new models\Attributes();
        $attributes->title = 'test';
        $attributes->entity_kind = 'entity'; 

        // Create an attribute_value
        $attribute_values = new models\Attribute_values();
        $attribute_values->value = 'test';
        $attribute_values->attribute_id = $attributes; 
        $attribute_values->entity_id = $entity; 

        //Save all records
        $attribute_values->save();

    }

after run it I have this error

Object of class Multiple\Frontend\Models\Attributes could not be converted to string Thanks



82.7k
Accepted
answer
edited Jul '14

The problem is assigning an object onto an attribute that expects a scalar:

$attribute_values->entity_id = $entity;

You can either assign the value to the relationship or assing the entity's id:

$attribute_values->entity = $entity; 
$attribute_values->entity_id = $entity->id;


410

I have the same problem, but I donĀ“t know, what exactly the entity property is. I have tried it with the alias of the Relationship, but it does not work.