I'll certainly have lots of questions on this group, I hope some of
you will be able to help me :-)
My first question is how access others models in a model?
Suppose I have a model named User and i'm in my Object model
For example i need to get the first User for some tests...
I have this:
class Object extends ActiveRecord
{
function user_1()
{
$obj = User->find(1);
//this line doesn't work
$attr = 'id';
return $obj->$attr;
}
}
I also tried things like
User.find(1)
$User->find(1)
nothing works!
Someone can help me doing this basic call?
Thanks
have you read the tutorial? ->
http://www.akelos.org/docs/tutorials/booklink
otherwise, if you really have a model 'user'.
load it:
Ak::import('User'); // or: require_once the class_file
instantiate a object:
$obj =& new User(1);
$obj->id;
or: $obj->getId();
I have read the tutorial sure, but there is no explication about my
problem there. All that's explain is how access a model in a
controller with help of var models= ...;
So i've got this working now:
--Object model------
function title()
{
Ak::import('User');
$obj = new User(1);
$name = 'name';
return $obj->$name;
}
But what i need is to do a find(conditions) on my User table.
User->find(1); still doesn't work
Also can you explain me just in some words the difference between $obj
= new User(1); and $obj =& new User(1);
Thanks
afaik, in Akelos, unlike in rails, find etc. aren't static methods. So you can't just call User->find(...) or User::find().
you always need a model object, so in your example it should work like
$obj->find( );
Hope that helps.
Cheers: Tom
I would say usually a Model doesnt have to access another model, except
they relate to each other.
In that case, we would have to declare that relationsship. Akelos knows 4
kinds of relating tables: belongs_to, has_one, has_many, finally
has_and_belongs_to_many (=many-many-relationship via a (implicit)
join-table).
In other words I dont know what youre trying to achieve. The code you
provided looks very unusual for a "newbie", it might be a right solution,
anyway, I just dont know.
> But what i need is to do a find(conditions) on my User table.
> User->find(1); still doesn't work
User->find(1); is not valid PHP
$User =& new User();
$A_User =& $User->find(1);
In conclusion, impossible to make a search on a table(other than the
table associated with the model we are in) in 1 line of code ?
> sewenig.vcf
> 1KDownload
I also wondered what the difference between = and =& was.
=& is the assignment by reference operator. If you're using php 4, $obj1 = $obj2 creates a copy of obj2 and assigns it
to obj1. $obj1 =& $obj2 only assigns a reference, no additional object is created.
In php5 however, = and =& behave equal in terms of object assignment.
Cheers,
Tom
The akelos xref doc describes the various find methods. Have a look under AkActiverecord here:
http://www.akelos.org/xref/nav.html?index.html
I'ill try to explain to both of you why in fact i needed to access an
other model in a model without relationship.
In my project i need to have one database who can serve multiple kinds
of websites depend on the way we fill it.
The main thing is that's a website where people can share objects
(that can be songs for a site, pictures for another,...), but remember
with the same DB structure...
So it's impossible to know by advance what will be share on the
website, and how much attributes will have their object model. I have
this
objects| id | user_id | option1 | option2 | option3 ...
------------------------------------------------------
| 1 | 42 | Ayo |songname| 3min
Now with this table (in this example with a song shared by user 42), i
can't know wich attribute is the title of the object, wich one is the
artist name and so on...
(it all about abstraction)
So i've a second table like this
mappings| id | option | name |
-------------------------
| 1 | option1 | artist |
| 2 | option2 | title |
Ok. i'm in my Object model now and i want to add a virtual column name
so i could write in my view <?= $obj->title() ?>
---Object model----
function title()
{
Ak::import('Mapping');
$obj = new Mapping(1);
$test = $obj->find('first',array('conditions' => "name =
'title'")); //found the mapping object with name = 'title'
$test2 = $test->option; //found what option is related to title
return $this->$test2; // return value of the option2 attribute of
my object model
}
Hope my explication is clear
best regards,
Steve
> sewenig.vcf
> 1KDownload
> sewenig.vcf
> 1KDownload
maybe you could save those mappings tables by just using meaningful column names and making use of methods like
getColumns()/getContentColumns() in AkActiverecord? There's a couple of such methods that give you arrays of column
names and the like.
cheers, Tom
The source code must work, independently of objects table
implementation
> sewenig.vcf
> 1KDownload
> objects| id | user_id | option1 | option2 | option3 ...
> ------------------------------------------------------
> | 1 | 42 | Ayo |songname| 3min
so we have a foreign key, and one can say a object belongs to a user.
probably a user shares many objects.
but object is a abstract thing. somewhere you have to define which
objects you have, that means which kind of "types" of "things" your
app implements. most probably you will have a Song, a Picture.
> mappings| id | option | name |
> -------------------------
> | 1 | option1 | artist |
> | 2 | option2 | title |
how does this look like for a picture?
| 3 | option1 | photographer |
> $test = $obj->find('first',array('conditions' => "name = 'title'"));
=> [id => 2, option => option2]
that doesnt mean anything to me
Yes that's it.
>how does this look like for a picture?
an application will share only one kind of object!
You put | 3 | option1 | photographer | , that's impossible, we can
only have one mapping of each option, in this case there will be two
mapping of option1.
If i go deeper in my explication, i can say that all kind of objects
will have some common attributes like title, description, year
But when a person will setup the website for the first time, he had to
describe what kind of object he will share, and what attributes and
there types.
For example, two person are setting up a website for sharing songs.
The first one say that a song has a name, artist, date.
The second one say that a song has a title, singer, year.
This is the same for me as a developer !!!
So i will use custom types and my mappings table will looks like :
For first one,
mappings| id | option | name | type |
---------------------------------
| 1 | option1 | name | title
| 2 | option2 | date | year
--> i'm now able to render : "Name of the song : songtitle goes here"
For second one,
mappings| id | option | name | type |
---------------------------------
| 1 | option1 | title | title
| 2 | option2 | year | year
--> i'm now able to render : "Title of the song : songtitle goes here"
Search will now be something like : $obj-
>find('first',array('conditions' => "type = 'title'"));
The difficulty is that people who will setup websites for same things
to share, can provide a different numbers of attributes and
attributes's names.
That's not so easy and I still have to explore some other possiblities
of database design...
Because as I say, it's all about abstraction.
For example the website will have a search page to search objects
shared on it.
We don't want to make the search possible with all attributes of
objects (the search form will be dynamically generated depend on
numbers of attr and numbers of attr that were described as
"searchable").
Maybe someone will just want a search by song title, and otherone will
want a search by song title, artist or year ...
Don't be affraid, i've several months to do this project :-)
on the other side there might be "free" properties, each user can add.
then an object would have many properties. a property would be a key/
value pair.
>
> This is the same for me as a developer !!!
> So i will use custom types and my mappings table will looks like :
>
> For first one,
> mappings| id | option | name | type |
> ---------------------------------
> | 1 | option1 | name | title
> | 2 | option2 | date | year
> --> i'm now able to render : "Name of the song : songtitle goes here"
> For second one,
> mappings| id | option | name | type |
> ---------------------------------
> | 1 | option1 | title | title
> | 2 | option2 | year | year
> --> i'm now able to render : "Title of the song : songtitle goes here"
on the same table ("mappings") the id is the primary key and therefore
unique. so what youre writing cant be the whole truth.
id | type(_id) | translate_to
1 | title (1) | title
2 | title (1) | name
3 | year (2) | date
4 | year (2) | year
option <=> type are hard-wired in your example.