Doctrine Cheatpost

It’s all about doctrine’s FAQ version of myself (this post will be updated occasionally)

Doctrine database CRUD (create, read, update, delete)

That has been explained in so much detail here. But I will explain some of them briefly.

1. Insert into on doctrine (create)

I’ll assume that you have created your models :

$u = new User();

$u->username = 'myuser';
$u->password = 'mypass';

$u->save();

or from array…

$data = array(
	'username' => 'myuser',
	'password' => 'mypass',
	'email' => 'my@email.com'
);

$u = new User();

$u->fromArray($data);

$u->save();

If you have created several objects then you don’t need to save() for each object, you should use

$u = new User();
$u->username = 'myuser';
$u->password = 'mypass';

$u2 = new User();
$u2->username = 'foouser';
$u2->password = 'foopass';
$conn = Doctrine_Manager::connection();
$conn->flush();

instead.

2. Select * from some_column on doctrine (Read)

// select by primary key, it returns an object
$user_id = 1;
$u = Doctrine::getTable('User')->find($user_id);
echo $u->username;

// findOneBy* well done, because doctrine will ignore sensitive case of *
// it will returns several objects
$username = 'myuser';
$u = Doctrine::getTable('User')->findOneByUsername($username);
echo $u->username;

// findBy* select several item 'where * = value'
$users = Doctrine::getTable('User')->findByStatus('active');
echo $users[0]->username;
echo $users[1]->username;

// using DQL (Doctrine Query Language), just like you write your own query
$status = 'active';
$q = Doctrine_Query::create()
	->select('username')
	->from('User')
	->where('status = ?', $status)
	->limit(20);
$users = $q->execute();
echo $users[0]->username;
echo $users[1]->username;

// you can convert the object into array like this...
$user_id = 1;
$u = Doctrine::getTable('User')->find($user_id);
$u_arr = $u->toArray();
print_r($u_arr);

3. Update some_column on doctrine (Update)

// Update by primary key.
// In this example,
// if your password attribute in the model has encrypted config such as MD5
// so by doing this script, the string will be automatically encrypted
$user_id = 1;
$u = Doctrine::getTable('User')->find($user_id);
$u->password = 'newpassword';
$u->save();

// update by using DQL (Doctrine Query Language), just like you write your own query
$status = 'active';
$q = Doctrine_Query::create()
	->update('User')
	->set('status', '?', $status)
	->where('id < 10'); $numrows = $q->execute();
echo "$numrows records updated";

4. Delete on doctrine (Delete)

// update by primary key
$user_id = 1;
$u = Doctrine::getTable('User')->find($user_id);
$u->delete();

// update by using DQL (Doctrine Query Language), just like you write your own query
$q = Doctrine_Query::create()
	->delete('User')
	->where('id < 10'); $numrows = $q->execute();
echo "$numrows records deleted";

Select distinct on doctrine

$q = Doctrine_Query::create()
 ->select('DISTINCT (tahun) as distinctyear')
 ->from('report');
$yearlist = $q->execute()->toArray();

echo '<pre>';
print_r($yearlist);
echo '</pre>';

will be..

Array
(
    [0] => Array
        (
            [id] =>
            [kota_id] =>
            [tahun] =>
            [notes] =>
            [lock_status] =>
            [distinctyear] => 2006
        )

    [1] => Array
        (
            [id] =>
            [kota_id] =>
            [tahun] =>
            [notes] =>
            [lock_status] =>
            [distinctyear] => 2010
        )

    [2] => Array
        (
            [id] =>
            [kota_id] =>
            [tahun] =>
            [notes] =>
            [lock_status] =>
            [distinctyear] => 2011
        )

)

Some Manipulation

//we can manipulate DQL something like this, just use your imagination
$q = Doctrine_Query::create()
     ->select('*')
     ->from('report');

//to check the query resulted just echo it
echo $q;
//we can concat them like this
$q = Doctrine_Query::create()
     ->select('*')
     ->from('report');
$q->where('status = ?','valid');
$q->andwhere('name = ?','joe');
$q->offset(0);
$q->limit(10);
echo $q;
// SELECT* FROM report WHERE status = 'valid' AND name = 'joe' LIMIT 0, 10
// We can use variable as an attribute of retrieved object
$q = Doctrine_Query::create()
     ->select('*')
     ->from('report');
$res = $q->execute();
$field = 'name';
echo $res->name;
//or
echo $res->$field

By the way, you can see the long documentation of some DQLs here.

It’s just the beginning… We can explore much more in doctrine, such as object manipulation, relationship manipulation, hydration, etc.

Satu pemikiran pada “Doctrine Cheatpost

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s