models: fundamental question referring to member variables

2010-10-03 Thread DerBjörn
Hi,

as a newbie to cakephp for me it looks like that 'normal' models don't
have any more common member variables, but associative arrays.

For example I have a model 'Person' with firstname, lastname, age etc.

Generally i would solve this with member variables $firstname,
$lastname and $age and their common getters and setters
setFirstname($value), getAge(), etc., but this still does have sense?
When i save a person i have to use an associative array, so with my
technique i need to create first an array ($data = array()) out of my
common member variables to pass it to model's function -save($data)

Does it mean that the common member variables are obsolete and i
actually only have to use one member variable $data and use it then
like:

function setFirstname($value){ $this-data['firstname'] = $value;}; or
function getAge(){ return $this-data['age']; }; ?

The same when i want to make a new instance of a Person and retrieve
their variables from the database:
Does it has to look like following then?

$person = new Person();
$person-retrieve(5); // ID

and the model Person has a function retrieve($id) when i get then its
data from the database and set it to $this-data??

I hope i made me explain at least a little and you understand my
question :)
Thanks for any advice or example!

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: models: fundamental question referring to member variables

2010-10-03 Thread Miles J
Theres no point for getters and setters if they dont do anything to
the property. If it just sets and gets without modifying the data in
any way, you might as well just use public properties. However, Cake
does support the active record approach.

$user = new User();
$user-username = 'miles';
$user-email = 'em...@domain.com';
$user-save();

On Oct 3, 1:12 pm, DerBjörn b.unkh...@googlemail.com wrote:
 Hi,

 as a newbie to cakephp for me it looks like that 'normal' models don't
 have any more common member variables, but associative arrays.

 For example I have a model 'Person' with firstname, lastname, age etc.

 Generally i would solve this with member variables $firstname,
 $lastname and $age and their common getters and setters
 setFirstname($value), getAge(), etc., but this still does have sense?
 When i save a person i have to use an associative array, so with my
 technique i need to create first an array ($data = array()) out of my
 common member variables to pass it to model's function -save($data)

 Does it mean that the common member variables are obsolete and i
 actually only have to use one member variable $data and use it then
 like:

 function setFirstname($value){ $this-data['firstname'] = $value;}; or
 function getAge(){ return $this-data['age']; }; ?

 The same when i want to make a new instance of a Person and retrieve
 their variables from the database:
 Does it has to look like following then?

 $person = new Person();
 $person-retrieve(5); // ID

 and the model Person has a function retrieve($id) when i get then its
 data from the database and set it to $this-data??

 I hope i made me explain at least a little and you understand my
 question :)
 Thanks for any advice or example!

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: models: fundamental question referring to member variables

2010-10-03 Thread DerBjörn
Your example and explaination I understand, but what about using other
classes as member variables and not just simple values.
Let me give you an example:
The user table has a column 'birthtime', but in my page i want to show
the age in years and days of the user instead.
So i have a class 'Age':

Pseudocode:
class User(
private $Age = new Age());

So when i retrieve the data of the database i first need to set the
birthtime:
$this-Age-setBirthtime($data['User']['birthtime']);
to be then able to access to calculated years and days:

$user-getAge()-getYears();
$user-getAge()-getDays();

The same the other way round:
If i want to randomize the birthtime of the user with my solution i am
able to do it like that:
$user-getAge()-randomizeBirthtime();

But when i want to save the user of course first i have to get the
random birthtime out of its class and put it in the array:
$this-data['User']['birthtime'] = $this-Age-getBirthtime();
$this-save($this-data);

Something like that, sorry about my pseudo code.
The problem is that the most of my member varables are classes
actually to be able to randomize or do calculations with their values.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: models: fundamental question referring to member variables

2010-10-03 Thread Jack Timmons
On Sun, Oct 3, 2010 at 6:56 PM, DerBjörn b.unkh...@googlemail.com wrote:
 The user table has a column 'birthtime', but in my page i want to show
 the age in years and days of the user instead.

The you should make a view helper for this, and not bother the data in-transit.

class AgeHelper extends AppHelper {} (I think, I don't have the
inclination ATM to ensure I'm accurate).

 So when i retrieve the data of the database i first need to set the
 birthtime:

 $this-Age-setBirthtime($data['User']['birthtime']);
 to be then able to access to calculated years and days:

 $user-getAge()-getYears();
 $user-getAge()-getDays();

 The same the other way round:
 If i want to randomize the birthtime of the user with my solution i am
 able to do it like that:
 $user-getAge()-randomizeBirthtime();

If you go with my Helper idea, I see no point in any of this. The data
should stay exactly as it came from the database, and your Helper
handle displaying how you want it.

 But when i want to save the user of course first i have to get the
 random birthtime out of its class and put it in the array:
 $this-data['User']['birthtime'] = $this-Age-getBirthtime();
 $this-save($this-data);

 Something like that, sorry about my pseudo code.
 The problem is that the most of my member varables are classes
 actually to be able to randomize or do calculations with their values.

And if you go with my method, you don't have to worry about messing
with the data at all. If they change it, you do the same calculations
in the model's beforeSave. Or the controller, whichever holds Cthulhu
off another night.

-- 
Jack Timmons
@_Codeacula

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en