delete() public method

Delete record with a specified id. If no ID is specified then current record is deleted.
public delete ( mixed $id = null )
$id mixed
Example #1
0
 public function testAtomicOperations()
 {
     $db = new Persistence_SQL($this->db->connection);
     $a = ['item' => [['name' => 'John'], ['name' => 'Sue'], ['name' => 'Smith']]];
     $this->setDB($a);
     $m = new Model($db, 'item');
     $m->addField('name');
     $m->load(2);
     $m->addHook('afterSave', function ($m) {
         throw new \Exception('Awful thing happened');
     });
     $m['name'] = 'XXX';
     try {
         $m->save();
     } catch (\Exception $e) {
     }
     $this->assertEquals('Sue', $this->getDB()['item'][2]['name']);
     $m->addHook('afterDelete', function ($m) {
         throw new \Exception('Awful thing happened');
     });
     try {
         $m->delete();
     } catch (\Exception $e) {
     }
     $this->assertEquals('Sue', $this->getDB()['item'][2]['name']);
 }
Example #2
0
 public function testDoubleReverseJoin()
 {
     $a = ['user' => [10 => ['id' => 10, 'name' => 'John 2', 'contact_id' => 100], 20 => ['id' => 20, 'name' => 'Peter', 'contact_id' => 100], 30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200], 40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300]], 'contact' => [100 => ['id' => 100, 'contact_phone' => '+555', 'country_id' => 1], 200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5]], 'country' => [1 => ['id' => 1, 'name' => 'UK'], 2 => ['id' => 2, 'name' => 'US'], 3 => ['id' => 3, 'name' => 'India']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $m_u = new Model($db, 'user');
     $m_u->addField('contact_id');
     $m_u->addField('name');
     $j = $m_u->join('contact');
     $j->addField('contact_phone');
     $c = $j->join('country');
     $c->addField('country_name', ['actual' => 'name']);
     $m_u->load(10);
     $m_u->delete();
     $m_u->loadBy('country_name', 'US');
     $this->assertEquals(30, $m_u->id);
     $this->assertEquals(['user' => [20 => ['id' => 20, 'name' => 'Peter', 'contact_id' => 100], 30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200], 40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300]], 'contact' => [200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5]], 'country' => [2 => ['id' => 2, 'name' => 'US'], 3 => ['id' => 3, 'name' => 'India']]], $this->getDB());
 }
Example #3
0
 public function testJoinDelete()
 {
     $a = ['user' => [1 => ['id' => 1, 'name' => 'John 2', 'contact_id' => 1], 2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1], 3 => ['id' => 3, 'name' => 'XX', 'contact_id' => 2], 4 => ['id' => 4, 'name' => 'YYY', 'contact_id' => 3]], 'contact' => [1 => ['id' => 1, 'contact_phone' => '+555'], 2 => ['id' => 2, 'contact_phone' => '+999'], 3 => ['id' => 3, 'contact_phone' => '+777']]];
     $db = new Persistence_Array($a);
     $m_u = new Model($db, 'user');
     $m_u->addField('contact_id');
     $m_u->addField('name');
     $j = $m_u->join('contact');
     $j->addField('contact_phone');
     $m_u->load(1);
     $m_u->delete();
     $this->assertEquals(['user' => [2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1], 3 => ['id' => 3, 'name' => 'XX', 'contact_id' => 2], 4 => ['id' => 4, 'name' => 'YYY', 'contact_id' => 3]], 'contact' => [2 => ['id' => 2, 'contact_phone' => '+999'], 3 => ['id' => 3, 'contact_phone' => '+777']]], $a);
 }