Seems like you have two questions. The first one is about making the association, the second seems to be about formatting the dates. Is that right?
First question first: Back up a bit. Are you able to load a single instance of Article, and get its association? Normally you would have the singular form of the model as the foreign key in the associated model. That is I would expect singular column names in your join table:
bonussystems_has_articles(article_id, bonussystem_id)
It's a join table, so you can do a quick test on the join by checking for the join model explicitly.
I'm am able to get one Article as an single Object from the id (Primary key) and rename my tablerows, but how can i save the association. Can you write some Example Code for me?
Assuming this table data:
Article model (table articles)| id | name | price |
| 1 | Article A | 5.00 |
| id | article_id | bonussystem_id |
| 5 | 1 | 7 |
What happens when you call this?
$article = Article::find(1); echo $article->bonussystems_has_article->id;
I renamed my Tablerows und wrote this Code into my Controller:
$bonussystem = new Bonussystem;
$bonussystem->name = $data['name'];
foreach($data['articles'] as $id => $val) {
if($val) $bonussystem->articles[] = Article::find($id);
}
var_dump($bonussystem);
My Classes(both) Contains the association description
class Article extends ActiveRecord\Model {
static $before_save = array('setImage_path');
static $after_save = array('upload');
static $before_destroy = array('remove_image');
static $setters = array('price');
static $getters = array('created_at', 'updated_at');
static $validates_presence_of = array(
array('title'),
array('price'),
);
static $has_many = array(
array('bonussystems',
'through' => 'bonussystems_has_articles',
'foreign_key' => 'bonussystems'
)
);
class Bonussystem extends ActiveRecord\Model {
static $getters = array('created_at', 'updated_at');
static $validates_presence_of = array(
array('name'),
);
static $has_many = array(
array('articles',
'through' => 'bonussystems_has_articles',
'foreign_key' => 'articles'
)
);
but i got this Error:
Uncaught exception 'ActiveRecord\HasManyThroughAssociationException' with message 'Could not find the association bonussystems_has_articles in model Bonussystem'
I want to create a new association. Not read.
How is your Bonussystems_has_article model configured? For has a has_many through association you need a model class for the "through" association.
I renanamed my table and create the n:m Class:
class Bonussystem2article extends ActiveRecord\Model {
static $belongs_to = array(
array('article'),
array('bonussystem')
);
}
Table:
bonussystem2articles
- id
- article_id
- bonussystem_id
class Bonussystem extends ActiveRecord\Model {
static $getters = array('created_at', 'updated_at');
static $validates_presence_of = array(
array('name'),
);
static $has_many = array(
array('bonussystem2articles'),
array('articles', 'through' => 'bonussystem2articles')
);
now it runs.
(1-6/6)
Subject: Problem with n:m Associations
Hi,
I have an problem with associations. I write an program with n:m associations.
the database looks like:
articles(id, name, description, price, ...)
bonussystems_has_articles(articles_id, bonussystems_id)
bonussystems(id, name, created_at, ...)
my Bonussystem class:
class Bonussystem extends ActiveRecord\Model {
static $getters = array('created_at', 'updated_at');
static $validates_presence_of = array(
array('name'),
);
static $has_many = array(
array('articles',
'through' => 'bonussystems_has_articles',
'foreign_key' => 'articles')
);
then i try this code:
$bonussystem = new Bonussystem;
$bonussystem->name = $data['title'];
foreach($data['articles'] as $id => $val) {
if($val) array_push($bonussystem->articles, array('id' => $id));
}
$data['articles'] is an Array with articles.id in key like:
array('1' => 1, '2' => 1, '4' => 1);
i got this error:
Fatal error: Uncaught exception 'ActiveRecord\HasManyThroughAssociationException' with message 'Could not find the association bonussystems_has_articles in model Bonussystem' in /var/www/tel_you/lib/activerecord/Relationship.php on line 464
whats wrong in my little program? can everyone help me?
thanks for any help.