tron ics Fri Sep 03 20:34:36 -0400 2010

Subject: Request: Optional Prefix for Model Classes to prevent Errors when using in existing projects

Hello,

Request: Optional Prefix for Model Classes to prevent Errors when using in existing projects

I think integrating phpactiverecords with other PHP frameworks will make those easier to understand and better performing. Imagine using this with Zend Framework, Cake, Qcodo, etc.
This would be beautiful :)

Only one little thing leads to BIG problems:
In an existing project there might already be a Class "car" defined and an existing Databasetable "car".

Now there is the problem that you run into that phpactiverecords relies on finding a Class in the Model named excactly like the Databasetable.

If we could define a prefix in the config, phpactiverecords could use all Classnames prefixed like "pAR_model_car" so it will never overlap with existing namespace.

Best regards,
tronics


Jacques Fuentes Tue Sep 07 20:15:20 -0400 2010

You can use the namespace feature found in PHP 5.3 (which PHP-AR uses for its own classes)

// tronics/MyModel.php

namespace Tronics;
class MyModel extends ActiveRecord\Model
{

}

This way you can put all of your models under a specific namespace "Tronics" and it won't interfere with another code base.

tron ics Tue Sep 07 21:48:16 -0400 2010

Thank you for your feedback.

I didn't think about this solution, it looks awesome.
Tried to implement this with the example database, yet there is a little problem.

Fatal error: Class 'Tronics\ActiveRecord\Model' not found in /webseiten/test_phpactiverecord/examples/orders/models/Order.php on line 6

Also when another Class with the name Order is included in the orders.php file there are additional errors:
Fatal error: Uncaught exception 'ActiveRecord\RelationshipException' with message ''Order' must extend from ActiveRecord\Model' in....

I hope I do not demand too much, yet ->It would be most helpful if you could explain how to adapt one of your shipped examples to use this namespace feature. :)

Best,
tronics

Jacques Fuentes Wed Sep 29 21:00:18 -0400 2010

Using the shipped examples probably isn't best; instead, you could try something very simple:

model dir:

/webseiten/my_app/models/tronics
/webseiten/my_app/models/tronics/user.php
/webseiten/my_app/models/tronics/post.php

  1. User.php
    namespace Tronics;
    class User extends \ActiveRecord\Model {
      static $has_many = array(array('posts'));
    }
    
  1. Post.php
    namespace Tronics;
    class Post extends \ActiveRecord\Model {
      static $belongs_to = array(array('user'));
    }
    
  1. test.php
    $user = Tronics\User::first();
    echo $user->posts[0]->title;
    
tron ics Thu Sep 30 12:24:32 -0400 2010

Thank you for that example, now I understand exactly how to do it :)
This opens incredibly cool possiblities for implementing with existing systems.
:)

(1-4/4)