Hello,
I'm trying to run the first example in the Storing related records documentation but all I get is an error message reading Object of class Artists could not be converted to string.
In case it could help, the following steps do produce the error:
First create mysql schema and tables:
CREATE SCHEMA TEST;
USE TEST;
CREATE TABLE Artists (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
country VARCHAR(45) NULL,
PRIMARY KEY (id)
);
CREATE TABLE Albums (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
artist INT UNSIGNED NOT NULL,
year YEAR NULL,
PRIMARY KEY (id),
CONSTRAINT fk_Albums_Artists
FOREIGN KEY (artist)
REFERENCES Artists (id)
);
Create a brand new project: $ phalcon project test
and edit database details in /app/config.php.
Create /app/models/Artists.php:
class Artists extends \Phalcon\Mvc\Model
{
public $id;
public $name;
public $country;
public function initialize() {
$this->hasMany('id', 'Albums', 'artist');
}
}
Create /app/models/Albums.php:
class Albums extends \Phalcon\Mvc\Model
{
public $id;
public $name;
public $artist;
public $year;
public function initialize() {
$this->belongsTo('artist', 'Artists', 'id');
}
}
Edit /controllers/IndexController.php with the code from the doc:
class IndexController extends ControllerBase
{
public function indexAction()
{
// Create an artist
$artist = new Artists();
$artist->name = 'Shinichi Osawa';
$artist->country = 'Japan';
// Create an album
$album = new Albums();
$album->name = 'The One';
$album->artist = $artist; //Assign the artist
$album->year = 2008;
//Save both records
$album->save(); // this line triggers the error
}
}
Run the page and get:
Catchable fatal error: Object of class Artists could not be converted to string
Thanks for any help.