Priyank Bolia
Priyank Bolia

Reputation: 14449

Framework for database manipulation

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.

Upvotes: 0

Views: 712

Answers (5)

galymzhan
galymzhan

Reputation: 5523

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

Upvotes: 1

Starx
Starx

Reputation: 79069

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.

Upvotes: 0

F21
F21

Reputation: 33441

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.

Upvotes: 1

Saiyam Patel
Saiyam Patel

Reputation: 1161

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

Thanks

Upvotes: 0

Endijs
Endijs

Reputation: 550

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

Upvotes: 0

Related Questions