0

Is there a PHP framework, that lets me write such code.

class Item
{
    private $_id; //primary key
    private $name;
    private $price;
    function __construct($name, $price) {
       $this->name = $name;
       $this->price = $price;
    }
}
...
$item = new Item(...);
db->Save($item);
...
db->Delete($item);
...
db->Update($item);
...
Item[] = db->Fetch($whereClause);

Yes Admins, I searched on Google, but I got lost in the vast sea (Doctrine, Propel, ActiveRecords,...), asked a different question on SO (which prohibited me from comparison), so I am asking differently now, which framework lets me write such simple code, without much additional coding, no XML, YAML. Or additionally how to write such if not there. Any pointers will be helpful. Off course I have PHP 5.3 and can use PDO, but performance is a concern (magic functions...), not extensive features list (we support this thing also, that you will never use).

OOPS: forgot to add, don't want a framework like CakePHP, etc. I am writing simple php without frameworks, want just some libraries that I can include into my project and call the above syntax. Its ok for me, if it requires a little SQL behind.

Priyank Bolia
  • 14,077
  • 14
  • 61
  • 82
  • possible duplicate of [Good PHP ORM Library?](http://stackoverflow.com/questions/108699/good-php-orm-library) – Quentin Mar 20 '12 at 09:53
  • Can't understand why close, I am asking a simple thing, this is my interface specification, or interface oriented design what ever it is called. Just wanted to know, from other experience, which tools best fit for my job. If you don't know, that a different thing, then saying "close". – Priyank Bolia Mar 20 '12 at 09:55
  • Again I am not comparing ORM, that is a hard thing. I already said, I don't compare on basic on features. I just want a tool that can generate/use such simple code for me with high performance. – Priyank Bolia Mar 20 '12 at 09:57
  • @PriyankBolia - you answered your question already. The pattern is called ActiveRecord. It's used by Object Relation Mappers such as Doctrine or Propel. Give one of them a go and don't worry too much about XML files, it's actually pretty simple to get hang of Propel for example. – N.B. Mar 20 '12 at 09:59
  • Also I don't want dynamic model generations or learning a new query language for Doctrine. Let me say something like working with simple objects that are persistent in DB. – Priyank Bolia Mar 20 '12 at 10:00
  • @N.B. I looked at Doctrine2 and somewhere it said it uses its own query language. Is that true. I don't think it provide such things as db->Save($item); – Priyank Bolia Mar 20 '12 at 10:02
  • 1
    Take a look at Propel then. It provides you methods that do exactly what you described (among other many other things). I don't have experience with Doctrine but since it's an ORM as well, I assume it definitely has similar functionality to Propel. – N.B. Mar 20 '12 at 10:07
  • @N.B. in Propel, can we get away with $author->FirstName ="Priyank" instead of $author->setFirstName("Priyank"); as mentioned on their website. – Priyank Bolia Mar 20 '12 at 10:25
  • 1
    You can edit the classes Propel generates and do whatever you like with them, but you're not going to suffer a huge performance penalty if you use a setter method. It's not a magic method afaik. – N.B. Mar 20 '12 at 10:37

5 Answers5

1

If you do not need a full application framework, for example, you might have written your own framework or if you are not using any framework, I can not recommend Redbean enough. On the plus side, Redbean also has bridges for frameworks such as Zend and CodeIgniter.

I like Redbean because it does not require much configuraion, no YAML, XML or other configuration. The library is very light and quite performant. The amount of features is about "just right", there are no superfluous features, but you do have all the things you need in there. The syntax is also very close to the one you want.

Now the downside: You need to adhere to strict table structures. For example, an ID column is need for link tables. Support for table name prefixes has also been removed in 3.0. However, I don't find these things to be important, so it isn't a huge issue for me.

If it is, you could even fork the library and modify it to your needs.

F21
  • 32,163
  • 26
  • 99
  • 170
  • R::$f->begin() ->select('*')->from('bean') ->where(' field1 = ? ')->put('a')->get('row'); – Priyank Bolia Mar 20 '12 at 10:12
  • The above type of queries is completely different from the type of queries(see in the question) that I want to write. – Priyank Bolia Mar 20 '12 at 10:13
  • No. The use of PHP-SQL mixed syntax is optional and not often used anyway. Often times, plain SQL is used: http://www.redbeanphp.com/manual/finding_beans Have a look at this page for its main syntax, which is very similiar to what you require: http://www.redbeanphp.com/manual/create_a_bean – F21 Mar 20 '12 at 10:16
  • Ok, so how will you write a simple thing in RedBean like: Item[] = db->Fetch("price > 100"); – Priyank Bolia Mar 20 '12 at 10:20
  • Assuming you want to retrieve data from a table called "products", then you would do: `$Items = R::find('products', 'price > 100');` Want SQL bindings? Do `$items = R::find('products', 'price > ?', array(100));` `R` is just a wrapper for the facade, so it can easily be changed to what you desire. – F21 Mar 20 '12 at 10:21
1

php-activerecord will probably fit your needs. Model classes should extend from ActiveRecord\Model:

<?php
class City extends ActiveRecord\Model {
  // Table name will be defaulted to 'cities', but you can override it
  static $table_name = 'mycities';

  // Primary key will be defaulted to 'id', you can override it too
  static $primary_key = 'city_id';

  // Associations
  static $belongs_to = array(
    array('country')
  );

  static $has_many = array(
    array('citizens', 'class_name' => 'User')
  );
}

// Creating
$city = new City(array('name' => 'New York'));
$city->save();

// Find City with id=5, output some data
$anotherCity = City::find(5);
echo $anotherCity->country->location;
foreach ($anotherCity->citizens as $citizen) {
  echo $citizen->full_name;
}

This is a very simple example, php-activerecord has some nice features:

  • Sensible conventions, you can reduce amount of code by following them (in fact, you should follow them if you're starting from scratch)
  • Validations to enforce business rules
  • CRUD for manipulating objects in OO-way
  • Callbacks - advanced pieces of code to associate with different lifetime events of your models
galymzhan
  • 5,505
  • 2
  • 29
  • 45
  • The one difference that I see, it is mapping to every column of the DB table, in my example, there is no magic methods, and I have explicitly define "private $price;". Doesn't your approach avoids code completion. – Priyank Bolia Mar 20 '12 at 10:17
  • @PriyankBolia You don't have to define properties in a model definition. Regarding code completion - I think [phpdoc comments](http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags.property.pkg.html) could solve that – galymzhan Mar 20 '12 at 10:22
0

I think a lot of them. ZendFramework is one of such frameworks.

Endijs
  • 550
  • 3
  • 7
0

There are many frameworks. See this for an example.

Every frameworks have there own way of API, through which you can use many features, such as db, xml.

  1. For ORM only Doctrine is very Excellent
  2. For rapid development and scaffolding features Codeigniter is very good.
  3. For in-depth framework with many pre-built libraries Zend-Framework is one of the best. It has libraries for Doctrine too.
Starx
  • 77,474
  • 47
  • 185
  • 261
0

cake PHP is very near to your requirement search scfolding with cake PHP is very near to your requirement

Thanks

Saiyam Patel
  • 1,161
  • 9
  • 18