How access others models in a model?

5 views
Skip to first unread message

Steve Verlinden

unread,
Nov 9, 2007, 8:04:10 PM11/9/07
to Akelos PHP Framework
Hi, I'm new here and i'll use akelos for a university project this
year.
I'm a Rails developer but unfortunatly i can't use it for that
project !

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

Kaste

unread,
Nov 9, 2007, 8:18:15 PM11/9/07
to akelos-f...@googlegroups.com
Hi,

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();

Steve Verlinden

unread,
Nov 10, 2007, 7:30:06 AM11/10/07
to Akelos PHP Framework
Thanks for you answer but i'm not already done.

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

Thomas Sewenig

unread,
Nov 10, 2007, 7:43:33 AM11/10/07
to akelos-f...@googlegroups.com
Hi Steve,

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

sewenig.vcf

Kaste

unread,
Nov 10, 2007, 7:56:43 AM11/10/07
to akelos-f...@googlegroups.com
I dont know exactly if youre on the spot.

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);


Steve Verlinden

unread,
Nov 10, 2007, 8:16:21 AM11/10/07
to Akelos PHP Framework
Thank you Thomas, that helps me !
I've the result i wanted with this :

--Object model------
function title()
{
Ak::import('User');
$obj = new User(1);
$test = $obj->find('first',array('conditions'
=> "name = 'steve'"));
...
}
But that's really not beautifull code :/
In fact the line
$obj = new User(1);
can be anything else like
$obj = new User(2);

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

Thomas Sewenig

unread,
Nov 10, 2007, 8:15:07 AM11/10/07
to akelos-f...@googlegroups.com
Hi Steve,

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

sewenig.vcf

Thomas Sewenig

unread,
Nov 10, 2007, 8:24:08 AM11/10/07
to akelos-f...@googlegroups.com
Hi Steve,

The akelos xref doc describes the various find methods. Have a look under AkActiverecord here:

http://www.akelos.org/xref/nav.html?index.html

sewenig.vcf

Steve Verlinden

unread,
Nov 10, 2007, 8:39:04 AM11/10/07
to Akelos PHP Framework
Ok i see, i work with php5.

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

Steve Verlinden

unread,
Nov 10, 2007, 8:53:39 AM11/10/07
to Akelos PHP Framework
Damn, i took time to write an entire explication with db schemas of
what i need to do but my last post is not on the discussion.
Do you also see n messages on the top, but if you scroll there is only
n-1 messages?

> sewenig.vcf
> 1KDownload

Thomas Sewenig

unread,
Nov 10, 2007, 8:55:28 AM11/10/07
to akelos-f...@googlegroups.com
Hi,

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

sewenig.vcf

Steve Verlinden

unread,
Nov 10, 2007, 9:03:24 AM11/10/07
to Akelos PHP Framework
can't use meaningful column names ...
because the objects table will not be the same depend on what will be
shared on the website

The source code must work, independently of objects table
implementation

> sewenig.vcf
> 1KDownload

Kast

unread,
Nov 10, 2007, 9:30:26 AM11/10/07
to Akelos PHP Framework
This is a thing about how to design your domain. Not how to implement
it, IMHO.

> 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

Steve Verlinden

unread,
Nov 10, 2007, 10:04:31 AM11/10/07
to Akelos PHP Framework
>so we have a foreign key, and one can say a object belongs to a user.
>probably a user shares many objects.

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 :-)

Kast

unread,
Nov 10, 2007, 11:01:48 AM11/10/07
to Akelos PHP Framework
> 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.
then this sounds like translating.
say, you have 5 properties a song can have. (in the end they
correspond to the tags of the actual mp3-file on your drive)
but someone calls the artist-property (or: tag) "artist", someone
calls it "Künstler".

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.

Steve Verlinden

unread,
Nov 10, 2007, 11:22:52 AM11/10/07
to akelos-f...@googlegroups.com
>then an object would have many properties. a property would be a key/
>value pair.

i will explore this way.


>on the same table ("mappings") the id is the primary key and therefore
>unique. so what youre writing cant be the whole truth

Sure id is the primary key! Read carefully , in my example it's two different mappings table in two different websites!

I stop working for today.
best regards,

Steve


2007/11/10, Kast <thd...@gmx.net>:
Reply all
Reply to author
Forward
0 new messages