@@ -431,7 +431,7 @@ take values of 123 and write it on top of 124?
431
431
432
432
Here is how::
433
433
434
- $m->load(123)->duplicate(124)->replace ();
434
+ $m->load(123)->duplicate()->setId( 124)->save ();
435
435
436
436
Now the record 124 will be replaced with the data taken from record 123.
437
437
For SQL that means calling 'replace into x'.
@@ -482,7 +482,7 @@ Start by creating a beforeSave handler for Order::
482
482
if ($this->isDirty('ref')) {
483
483
484
484
if (
485
- $this->newInstance( )
485
+ (new static() )
486
486
->addCondition('client_id', $this->get('client_id')) // same client
487
487
->addCondition($this->id_field, '!=', $this->getId()) // has another order
488
488
->tryLoadBy('ref', $this->get('ref')) // with same ref
@@ -537,7 +537,7 @@ The other, more appropriate option is to re-use a vanilla Order record::
537
537
function archive() {
538
538
$this->save(); // just to be sure, no dirty stuff is left over
539
539
540
- $archive = $this->newInstance( );
540
+ $archive = (new static() );
541
541
$archive->load($this->getId());
542
542
$archive->set('is_archived', true);
543
543
@@ -546,75 +546,6 @@ The other, more appropriate option is to re-use a vanilla Order record::
546
546
return $archive;
547
547
}
548
548
549
- This method may still not work if you extend and use "ActiveOrder" as your
550
- model. In this case you should pass the class to newInstance()::
551
-
552
- $archive = $this->newInstance('Order');
553
- // or
554
- $archive = $this->newInstance(new Order());
555
- // or with passing some default properties:
556
- $archive = $this->newInstance([new Order(), 'audit'=>true]);
557
-
558
-
559
- In this case newInstance() would just associate passed class with the
560
- persistence pretty much identical to::
561
-
562
- $archive = new Order($this->persistence);
563
-
564
- The use of newInstance() however requires you to load the model which is
565
- an extra database query.
566
-
567
- Using Model casting and saveAs
568
- ------------------------------
569
-
570
- There is another method that can help with escaping the DataSet that does not
571
- involve record loading:
572
-
573
- .. php :method :: asModel($class = null, $options = [])
574
-
575
- Changes the class of a model, while keeping all the loaded and dirty
576
- values.
577
-
578
- The above example would then work like this::
579
-
580
- function archive() {
581
- $this->save(); // just to be sure, no dirty stuff is left over
582
-
583
- $archive = $o->asModel('Order');
584
- $archive->set('is_archived', true);
585
-
586
- $this->unload(); // active record is no longer accessible.
587
-
588
- return $archive;
589
- }
590
-
591
- Note that after saving 'Order' it may attempt to :ref: `load_after_save ` just
592
- to ensure that stored model is a valid 'Order'.
593
-
594
- .. php :method :: saveAs($class = null, $options= [])
595
-
596
- Save record into the database, using a different class for a model.
597
-
598
- As in my archiving example, here is how we can eliminate need of archive()
599
- method altogether::
600
-
601
- $o = new ActiveOrder($db);
602
- $o->load(123);
603
-
604
- $o->set('is_arhived', true)->saveAs('Order');
605
-
606
- Currently the implementation of saveAs is rather trivial, but in the future
607
- versions of Agile Data you may be able to do this::
608
-
609
- // MAY NOT WORK YET
610
- $o = new ActiveOrder($db);
611
- $o->load(123);
612
-
613
- $o->saveAs('ArchivedOrder');
614
-
615
- Of course - instead of using 'Order' you can also specify the object
616
- with `new Order() `.
617
-
618
549
619
550
Working with Multiple Persistences
620
551
==================================
@@ -659,7 +590,7 @@ application::
659
590
$m = $this->sql->add(clone $class)->load($id);
660
591
661
592
// store into MemCache too
662
- $m = $m->withPersistence($this->mdb)->replace ();
593
+ $m = $m->withPersistence($this->mdb)->save ();
663
594
}
664
595
665
596
$m->onHook(Model::HOOK_BEFORE_SAVE, function($m){
@@ -697,7 +628,7 @@ cache::
697
628
$m = $this->sql->add(clone $class)->load($id);
698
629
699
630
// store into MemCache too
700
- $m = $m->withPersistence($this->mdb)->replace ();
631
+ $m = $m->withPersistence($this->mdb)->save ();
701
632
}
702
633
703
634
Load the record from the SQL database and store it into $m. Next, save $m into
0 commit comments