Re: saving hasAndBelongsToMany - what´s wrong?
Thanks ralph! I made a ticket for it on Trac! Hope they take it. greets felle42 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~--~~~~--~~--~--~---
Re: saving hasAndBelongsToMany - what´s wrong?
ok, I found it! the __saveMulti -method in the model class shuffels the $key=>$value association of the given data if there are some keys of the dependent model not given! This is because __saveMulti expects to be given all dependent-keys for that model. But if we have a 0:n-relationship it might be that some dependent keys are not set. Therefore the arrays in __saveMulti must be reference by associative keys. This is my new version of save_multi: (any comments welcome) /** * Saves model hasAndBelongsToMany data to the database. * * @param array $joined Data to save. * @param string $id * @return * @access private */ function __saveMulti($joined, $id) { $db =& ConnectionManager::getDataSource($this->useDbConfig); foreach($joined as $x => $y) { foreach($y as $assoc => $value) { $joinTable[$assoc] = $this->hasAndBelongsToMany[$assoc]['joinTable']; $mainKey[$assoc] = $this->hasAndBelongsToMany[$assoc]['foreignKey']; $keys[]= $this->hasAndBelongsToMany[$assoc]['foreignKey']; $keys[]= $this->hasAndBelongsToMany[$assoc]['associationForeignKey']; $fields[$assoc] = join(',', $keys); unset($keys); foreach($value as $update) { if (!empty($update)) { $values[] = $db->value($id, $this->getColumnType($this->primaryKey)); $values[] = $db->value($update); $values= join(',', $values); $newValues[] = "({$values})"; unset ($values); } } if (!empty($newValues)) { $newValue[$assoc] = $newValues; unset($newValues); } else { $newValue[$assoc]=array(); } } } $total = count($joinTable); if(is_array($newValue)){ foreach ($newValue as $loop_assoc=>$val) { $db =& ConnectionManager::getDataSource($this->useDbConfig); $table = $db->name($db->fullTableName($joinTable[$loop_assoc])); $db->execute("DELETE FROM {$table} WHERE {$mainKey[$loop_assoc]} = '{$id}'"); if (!empty($newValue[$loop_assoc])) { $secondCount = count($newValue[$loop_assoc]); for($x = 0; $x < $secondCount; $x++) { $db->execute("INSERT INTO {$table} ({$fields[$loop_assoc]}) VALUES {$newValue[$loop_assoc][$x]}"); } } } } } greets felle --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~--~~~~--~~--~--~---
saving hasAndBelongsToMany - what´s wrong?
Hello I want to assign a contact person(german: "Ansprechpartner") to different kinds of a model. my model looks like this: class Ansprechpartner extends AppModel { var $name = 'Ansprechpartner'; var $useTable = "ansprechpartner"; var$primaryKey = "ans_id"; var $recursive = 2; var $hasAndBelongsToMany = array( 'Kunde' => array('className' => 'Kunde', 'joinTable' => 'kunden_ansprechpartner', 'foreignKey' => 'ansprechpartner_id', 'associationForeignKey'=> 'kunden_id', 'uniq' => true), 'Traeger' => array('className' => 'Traeger', 'joinTable' => 'traeger_ansprechpartner', 'foreignKey' => 'ansprechpartner_id', 'associationForeignKey'=> 'traeger_id', 'uniq' => true, ), 'Verbund' => array('className' => 'Verbund', 'joinTable' => 'verbunde_ansprechpartner', 'foreignKey' => 'ansprechpartner_id', 'associationForeignKey'=> 'verbunde_id', 'uniq' => true, ) ); } one of the dependend models look like this ("Kunden" and "Verbund" look the same) class Traeger extends AppModel { var $useTable="traeger"; var $primaryKey="t_id"; var $name="Traeger"; var $displayField="t_name"; var $hasMany=array("Traegerzugehoerigkeit"=> array("className"=>"Traegerzugehoerigkeit", "foreignKey"=>"t_id")); var $hasAndBelongsToMany = array( 'Ansprechpartner' => array('className' => 'Ansprechpartner', 'joinTable' => 'traeger_ansprechpartner', 'foreignKey' => 'traeger_id', 'associationForeignKey'=> 'ansprechpartner_id', 'uniq' => true)); } One thing is, that only one of the dependend models id is given. My controller which saves the "Ansprechpartner" looks like this: // only the saving function shown: function view($id=null) { if(empty($this->params['data'])) { // edit record } else { // insert selector to which model should this Ansprechpartner be assigned // default should be Kunde $this->params['data']['Ansprechpartnerfunktion']['Ansprechpartnerfunktion']=array_unique(explode("|",$this->params['data']['Ansprechpartner']['selfunktionen'])); if(isset($this->params['data']['Traeger']) && strlen($this->params['data']['Traeger']['Traeger'])) { // if isset save to traeger $t_id=$this->params['data']['Traeger']['Traeger']; $this->params['data']['Traeger']['Traeger']=array($this->params['data']['Traeger']['Traeger']); print_r($this->params['data']['Traeger']['Traeger']); print_r($this->params['data']); $redirect="/Traeger/show/".$t_id; } else if(isset($this->params['data']['Verbund']) && strlen($this->params['data']['Verbund']['Verbund'])){ //if isset save to verbund // not implemented yet } else { // default save to kunde $this->params['data']['Kunde']['Kunde']=array($this->params['data']['Kunde']['Kunde']); $redirect="/Kunden/show"; } print_r($this->params['data']); $this->Ansprechpartner->Save($this->params['data']); exit; $this->redirect($redirect); }
Re: findall-problem with condition on child table
Hi Andy, have you already found out what that problem was? it seems that I have the same problem and also no idea... greets felle42 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~--~~~~--~~--~--~---
Re: hasAndBelongsToMany Manual error
There´s also another error in the same sector. I found out that I have to provide an array for the associated model. Giving only one ID(not as an array) didn´t work. Perhaps this is already fixed in the 1.1.3.x version of the framework. I haven´t tested it yet. from the manual of HABTM-documentation: "The submitted data must be a single ID, or an array of IDs of linked records." greets felle42 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~--~~~~--~~--~--~---
Re: Global variables?
Thanks 100rk. setting the variables in bootstrap.php global did it for me! But why do I have to do this? isn´t the file(bootstrap.php) included by a php-include or -require? I need the global varibals for implementing submenues. I define them as an array an the controller selects the right one. is there a better way of doing this? greets felle42 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~--~~~~--~~--~--~---
Global variables?
Hello, I'm new to CakePHP. I found some comments on how to access global variables so i've put an array-variable into the /app/conf/bootstrap.php but i can´t access it in my AppController::beforeFilter()-Method. The file(bootstrap.php) is loaded by the framework. I have verified that by putting a php-syntax error in it. But I can´t access the variables defined in it. I know I have to set them global where I try to access them... I use version 1.0.x.x of the framework. Any ideas? greets felle42 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~--~~~~--~~--~--~---