Welcome Guest, Not a member yet? Register   Sign In
IgnitedRecord 1.0 pre-release
#21

[eluser]adwin[/eluser]
@m4rw3r:
I have a questions.
my table was not designed having field like user_id, order_id, etc ... i just simply write like userid, orderid, etc ...

can your orm support this without changing your code ?

thanks
#22

[eluser]ben_co[/eluser]
Good question. Most of ORM libraries are indeed based on assumptions made on your DB schemas.

I have a similar question, what about primary keys lists (for instance: a pair "id,value") ? Futhermore, if there is, for instance, a relationship many_to_manybetween a table (PK: "t1_id") and the first one (PK: "t0_id","t0_value"), how can I declare it in my models ?

In fact, the real problem in this special case is also "identifying the keys (foreign or primary)" and not only the relationships... Maybe it would great to have some parameters/functions to do it with your library ?

PS: Sorry for my bad English if I made some mistakes ;-P ...
#23

[eluser]adwin[/eluser]
[quote author="ben_co" date="1210334056"]Good question. Most of ORM libraries are indeed based on assumptions made on your DB schemas.

I have a similar question, what about primary keys lists (for instance: a pair "id,value") ? Futhermore, if there is, for instance, a relationship many_to_manybetween a table (PK: "t1_id") and the first one (PK: "t0_id","t0_value"), how can I declare it in my models ?

In fact, the real problem in this special case is also "identifying the keys (foreign or primary)" and not only the relationships... Maybe it would great to have some parameters/functions to do it with your library ?

PS: Sorry for my bad English if I made some mistakes ;-P ...[/quote]

I know that's not easy ... but if we have to follow your pattern, the old database cannot use your lib just because their naming conventions not following 'rails' ...

how about like this, you simply declare the pattern is using '_id' but we can change for example just 'id' or anything else like '_fkid'

or you can make a definition like the relationship like
Code:
$this->have_many('students')->defineForeignKey('studentsid');
$this->have_many('students'); // if ForeignKey(FK) not defined then let it said join by students_id

PS: my english was not better than you :p
#24

[eluser]m4rw3r[/eluser]
By using the more "advanced" way of declaring the relations in the models you can set table name, relation name, foreign key column name, related model name and in the case many to many: also the relation table (and both of the foreign keys).
If you omit any of the settings (except for the tablename), the default values will be used for those settings omitted.

This is covered in the manual, so I'll post an example:
Code:
class project extends IgnitedRecord{
    var $__id_col = 'pk'; // primary key (default: id)
    var $__has_one = array('table' => 'supervisors',
                           'col' => 'supervisorid'); // foreign key column name
    var $__has_and_belongs_to_many =
        array( // multiple relations
              array('table' => 'developers',
                    'rel_table' => 'project_assignments', // tablename for the table storing the relations
                    'this_col' => 'projectid', // the foreign key column in the relation table referring to this table
                    'other_col' => 'developerid' // the foreign key column in the relation table referring to the related table
                   ),
              'clients' // just use all defaults
        );
    function project(){
        parent::IgnitedRecord();
    }
}

Currently my lib does not autodetect primary key name, personally I think it isn't needed (the __id_col property is fine).

I think I will implement some methods to change the relation settings (methods are needed because they are sanitized/normalized, not needed for the rest of the properties, so you can add/remove/edit relations during runtime), but currently you can change them in the property declarations (if you look at the constructor in IgnitedRecord, you can maybe modify the relations yourselves during runtime, but I'll make some methods soon).
#25

[eluser]adwin[/eluser]
[quote author="m4rw3r" date="1210347553"]By using the more "advanced" way of declaring the relations in the models you can set table name, relation name, foreign key column name, related model name and in the case many to many: also the relation table (and both of the foreign keys).
If you omit any of the settings (except for the tablename), the default values will be used for those settings omitted.

This is covered in the manual, so I'll post an example:
Code:
class project extends IgnitedRecord{
    var $__id_col = 'pk'; // primary key (default: id)
    var $__has_one = array('table' => 'supervisors',
                           'col' => 'supervisorid'); // foreign key column name
    var $__has_and_belongs_to_many =
        array( // multiple relations
              array('table' => 'developers',
                    'rel_table' => 'project_assignments', // tablename for the table storing the relations
                    'this_col' => 'projectid', // the foreign key column in the relation table referring to this table
                    'other_col' => 'developerid' // the foreign key column in the relation table referring to the related table
                   ),
              'clients' // just use all defaults
        );
    function project(){
        parent::IgnitedRecord();
    }
}

Currently my lib does not autodetect primary key name, personally I think it isn't needed (the __id_col property is fine).

I think I will implement some methods to change the relation settings (methods are needed because they are sanitized/normalized, not needed for the rest of the properties, so you can add/remove/edit relations during runtime), but currently you can change them in the property declarations (if you look at the constructor in IgnitedRecord, you can maybe modify the relations yourselves during runtime, but I'll make some methods soon).[/quote]

so changing the foreign key is supported at the moment ? that's great ... I am going to try. Btw, when are you going to release Final Version of 1.0 ? Smile cannot wait for that Big Grin
#26

[eluser]iNETZO[/eluser]
I realy like this model. I'm only having some problems with the relations.

I've got these two models:

<?php
class site extends IgnitedRecord {

// Constructor
public function __construct() {
// Call the Model constructor
parent::__construct();
$this->__table = "sites";
$this->__has_many = "sites_aliases";
}

}
?>

<?php
class sitealias extends IgnitedRecord {

// Constructor
public function __construct() {
// Call the Model constructor
parent::__construct();
$this->__table = "sites_aliases";
$this->__belongs_to = "sites";
}

}
?>

Controller code:

$site = $this->site->find(1);
$sitealiases = $site->get_related('sites_aliases');


Probably $this->__belongs_to and $this->__has_many need to be arrays but can't find the specification in the documentation. What do I have to do to make it work?
#27

[eluser]m4rw3r[/eluser]
@iNETZO:
1. In the case of the tablename in the site model, there is no need to declare it explicitly (page maps to pages, thanks to the CI inflector helper).

2. The relation to sites_aliases tries to find a model with the name sites_alias or sites_aliases, so you have to define the model name in the relation:
Code:
var $__has_many = array('table' => 'sites_aliases', 'model' => 'sitealias');

This is described in the manual under the relations "chapter", so I don't know if i made it clear enough, should I try to simplify that part?

@adwin:
Yes it is supported (forgot to mention that, I see that you figured :-) ).

I have no idea when I'll be finished with the sharp 0.1, but I feel that it is not so much left (maybe this with joins, but it seems like it is hard to implement in a way that allows a lot of flexibility, like the existing methods does (with CI's ActiveRecord)).

But suggestions are still welcome! Tongue
#28

[eluser]iNETZO[/eluser]
Thank you very much m4rw3r for your help. Unfornately it's still not working for me. When i'am looking at line 1069 it will never be true because the tablename is not an arraykey.

if($rel_name == false || in_array($rel_name,array_keys($this->__has_many))){
#29

[eluser]ben_co[/eluser]
[quote author="adwin" date="1210337092"]
how about like this, you simply declare the pattern is using '_id' but we can change for example just 'id' or anything else like '_fkid' or you can make a definition like the relationship like ...
[/quote]

I think you didn't catch what I want explain... But I must say I wasn't neither really clear %-P.

Indeed, I could simply rename the PK to follow the "rails" conventions. But one table of the DB has a pair as PK. To be more precise, I have multilingual datas in that table. So, I declare a pair PKSadid, language) to group all translations of a same content more easily. I thought It was quicker than specifying another column that have the role of grouping.
#30

[eluser]m4rw3r[/eluser]
@iNETZO:
IgnitedRecord have some methods that normalizes the data for the relations, so the relation name becomes the array key.
Only if you assign the properties AFTER the constructor (IgnitedRecord()), you have to use the internal format (and that does not have defaults, the normalization methods takes care of that).

In the next version I'll have some methods for assigning those properties after the constructor has done its work.

@ben_co:
Are you saying that your table structure is something like this?
Code:
id   language    text
1    en          Welcome!
1    sv          Välkommen!
2    en          Log out
2    sv          Logga ut
...  ...         ...
If so I believe that IgnitedRecord currently does not support it (not for the moment, at least).
But you can still use IgnitedRecord to fetch them, but saving/deleting may cause some great problems (deleting all rows with id 1, among other things).

I think it would be quite easy to implement it, but it may cause some troubles when it comes to relations (don't know, haven't tried yet).




Theme © iAndrew 2016 - Forum software by © MyBB