[INVALID] Update 2nd block table after adding block (bID)

Permalink 1 user found helpful
!!! This question is invalid. After extensive debugging, $this->bID got populated (I guess due to naming convention violation in the blocks' db.xml file).

Guys, I might be missing something, but I lost the trail somewhere...

I'm trying to figure out how to update a second table after adding a block. This second table needs a block ID (bID) to have the information coupled.

Can't find anything useful in the save() methods, nor in other files. Anybody?

kaspera
 
kaspera replied on at Permalink Reply
kaspera
I found _something_:

$b = $this->getBlockObject();
      $c = $b->getBlockCollectionObject();
      $db = Loader::db();
      if (intval($this->bID) > 0) {
         $total = $db->getOne("select count(*) as total from {$this->btTable} where bID = " . intval($this->bID));
      } else {
         $total = 0;
      }


Seems a bit iffy to me, but if that's the "official" way to go, I'll just stick to it...
mkly replied on at Permalink Reply
mkly
Typically in controller.php of your block you would could do this.

public function save($args) {
  $db = Loader::db();
  $db->Replace(
    'otherBlockTable',
    array(
      'id' => $args['other_id'],
      'bID' => $this->record->bID,
      'food' => $args['food'],
      'beverage' => $args['beverage']
    ),
    'id',
    true
  );
  parent::save($args0;
}
kaspera replied on at Permalink Reply
kaspera
The thing is: there is no block ID at the moment of adding a new block. There's only a new instance. $this->bID or $this->record->bID (depending on context) doesn't have anything yet to point at...

Only AFTER the block has been saved (and a DB record created), would it be possible to do stuff, but the save method doesn't return anything (NULL if you like). If the save method would have returned the newly inserted ID, it would at least have given me some more options.

Does concrete5 provide something similar to what PostgreSQL does when you query: "SELECT nextval ('id_seq');"? Or is there some other method/hook I could hack into?
kaspera replied on at Permalink Reply
kaspera
Hmm, strange things are happening today.
After I debugged some lines, I got hold of a blockID (bID) (even before insertion!).

That renders my question invalid since there IS a bID.

Sorry folks for the inconvenience and thanks for the much appreciated help!
mnakalay replied on at Permalink Reply
mnakalay
Hi,
Several blocks actually need to be able to do that, have a master/detail set of tables.
Personally I found the answer by looking at the controller of the default slideshow block that comes with C5, in the save function.

How it works is that you have to save things manually in the second table using a sql "INSERT INTO" command.
The code in the slideshow block is pretty easy to understand.
Ask again if anything is not clear.

Good luck
kaspera replied on at Permalink Reply
kaspera
AFAIK the slideshow block (hehe, always reminds of some Bob) does the insert of the master prior to having the extra's filled in. I'm trying to achieve this in one go: have the block and extra's being inserted in one go.

Please correct me if I'm wrong...
mnakalay replied on at Permalink Reply
mnakalay
well you would have to do an insert into for both tables and bypass the parent::save($args); that takes care of the master table, but that would just complicate things in my opinion. The 2 tables are not really linked, not like in a Microsoft Access database, any relationships has to be maintained by you through code. So I think the best way to do it is let C5 deal with the master table the way it's suppose to and write the code for the detail table yourself.
It's all executed in the same function, so really it's all done at once.