4

is it posible to have a true Model Layer in PHP like in Ruby on Rails? With Zend Framework you can create a Model but this is a class. As I know you have to write all the logic by myself.

Solutions?

xpepermint
  • 35,055
  • 30
  • 109
  • 163
  • 1
    ZF follows the opposite paradigm of Rails, that is, ZF is 'configuration over convention' rather than the opposite. Lots is left up to the developer... like the whole model layer. This is why I like it. – jason Jul 29 '09 at 16:41
  • 1
    old thread I know - but RoR (and many other frameworks) have pervaded the myth that Model classes map 1:1 to data tables. Model classes are primarily concerned with...well, modelling something from the real world in code. Models are not primarily meant to be a means of data persistence. The Active Record pattern favoured by RoR blurs the distinction between Models and Data Access Objects and so much of the web development community believe they are one and the same. – sunwukung Nov 28 '10 at 13:20
  • 1
    @sunwukung It is true that models and dbaccess are not intended to be 1:1, but for the *vast* majority of web applications the simplicity of having them be thus saves time and headaches. For most web applications you don't really need models that represent real world objects. What you need are models that represent DB tables. Most web applications are thin clients between a user and a DB. – Daniel Bingham Mar 10 '11 at 15:34
  • I know this is an old question, but if you're still looking for a Rails like PHP framework, take a look at CakePHP. I don't know if they were really around when you asked this. – Daniel Bingham Mar 10 '11 at 15:36

3 Answers3

3

True, in Zend Framework you need to declare classes for the database tables you'd like to access. But the rest can be done implicitly, if the default behavious is sufficient. The following is a valid and functional model class (compare to http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.introduction):

class bugs extends Zend_Db_Table_Abstract
{
    // table name matches class name
}

This should let you access the table named "bugs", e.g.:

$table = new bugs();
$data = array(
    'created_on'      => '2007-03-22',
    'bug_description' => 'Something wrong',
    'bug_status'      => 'NEW'
);
$table->insert($data);

Again, the example was taken directly from the documentation mentioned above.

cg.
  • 3,648
  • 2
  • 26
  • 30
2

Or since 1.8.x there is a DataMapper pattern used for models (see quickstart in manual)

Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82
1

i wrote a script that might suite your needs.

http://code.google.com/p/zend-db-model-generator/

ufk
  • 30,912
  • 70
  • 235
  • 386