Just a rough intro. Our system just needs users to be able to register their halls within our system and then modify details when needed. We are fine with most things except we cant get things that require many-many relationships to work because the data doesn't seem to be saving in our database. The code for our halls is below. Edit halls: Users can modify the pricing of a hall TABLE Hall Pricing(id, price_name) HallsHasPricing(joinTable)(id, halls_id, price_id, price_fee) PHP: The controller for this hall is HallsController.php php foreach($pricings as $pricing) { $price_fee = ""; $hmID = $pricing['Pricing']['id']; $price_fee = $pricing['HallsHasPricing']['price_fee']; echo "<tr>"; echo "<td>".$pricing['Pricing']['price_name']."</td>"; echo "<td>"; echo $this->Form->input("pricing.textbox.$hmID", array('label' =>'','size'=>'50', 'value'=>$price_fee)); echo "</td>"; echo "</tr>"; $price_fee = ""; } ?> </table> PHP: This is the code for the view of the Halls page itself Halls/view.ctp < ? php echo "<p />"; echo "<table cellpadding='1' width='50%'>"; foreach($hall['Pricing'] as $pricing) { echo "<tr>"; echo "<td>".$pricing['price_name']."<br /></td>"; echo "<td>".$pricing['HallsHasPricing']['price_fee']."<br /></td>"; echo "</tr>"; } echo "</table>"; ?> PHP:
I think I'd need to know more about your object structure -- it seems most queer to be calling query as a method of a data-set thus: $this->Hall->query( The 'Hall' property REALLY doesn't strike me as something that should be a database object... It's also hard to make sense of your run-on stuff it all on one line queries, and decrypting query issues without actually seeing the database structures adds to the difficulty. In general, most of the sub-code that's sending markup is a laundry list of how not to build php output -- with the multiple back to back echo statements for nothung, presence of double quotes making you work harder for harder for no good reason, and of course nothing resembling sane easy to debug formatting. I will say that if you're going have a 'hall' object able to be handling queries, the queries should be 'prepare/exec' style in those objects, NOT as standalone queries in your main code.
It's cakephp and having a hall as a database object is fine. It's something that things happen to. What I don't recommend, however, is touching cake's M:M handler. It just seems flawed and normally when you get into your project you will find you need a linking record anyway. For instance with Pricing - and I'd have thought that would just be a child record anyway, I can't see how Price is a thing. But with pricing you need to know who set the price, what date, is the price active? or has the price been withdrawn? If a price isn't active you might want to hide it but you'd never delete it or else it will muck up the booking history etc.
TABLE Hall(id) Pricing(id, price_name) HallsHasPricing(joinTable)(id, halls_id, price_id, price_fee) The controller for this hall is HallsController.php edit method echo "<br />"; foreach($this->data['pricing']['textbox'] as $k=>$v) { if ($v) { $this->request->data['Pricing'][$k]['id'] = $k; $this->request->data['Pricing'][$k]['price_fee'] = $v; } else { $this->Hall->query("DELETE FROM halls_has_pricings WHERE halls_id =$id"); } } if ($this->Hall->saveAll($this->request->data)) { $this->Session->setFlash(__('The hall has been saved')); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The hall could not be saved. Please, try again.')); } $pricings = $this->Hall->query("SELECT Pricing.id, Pricing.price_name, HallsHasPricing.id, HallsHasPricing.halls_id, HallsHasPricing.price_fee FROM pricings AS Pricing LEFT JOIN halls_has_pricings AS HallsHasPricing ON (HallsHasPricing.halls_id = $id AND HallsHasPricing.pricing_id = Pricing.id) ORDER BY price_name ASC"); PHP: This is the view for the edit page of halls Halls/edit.ctp <tablecellpadding='1'width='50%'> <? php foreach($pricings as $pricing) { $price_fee = ""; $hmID = $pricing['Pricing']['id']; $price_fee = $pricing['HallsHasPricing']['price_fee']; echo "<tr>"; echo "<td>".$pricing['Pricing']['price_name']."</td>"; echo "<td>"; echo $this->Form->input("pricing.textbox.$hmID", array('label' =>'','size'=>'50', 'value'=>$price_fee)); echo "</td>"; echo "</tr>"; $price_fee = ""; } ?> </table> PHP: This is the code for the view of the Halls page itself Halls/view.ctp <?php? echo "<p />"; echo "<table cellpadding='1' width='50%'>"; foreach($hall['Pricing'] as $pricing) { echo "<tr>"; echo "<td>".$pricing['price_name']."<br /></td>"; echo "<td>".$pricing['HallsHasPricing']['price_fee']."<br /></td>"; echo "</tr>"; } echo "</table>"; ?> PHP:
I just put my code again, and the problem is there are no error message shows out, and database work fine, we can direct insert data into jointable by using phpmyadmin, and the data will also show on the webpage, but the problem is when we change data or insert more data by using website edit action, it would clean all the data in the jointable, it just keep store Null to the jointable
Ok, so you've reviewed your database structure and requirements and you reckon you have to use HABTM? bummer I use ->query alot because I have some funky stuff going on in my main project but I don't get why you are for what seem like very standard queries that the model should be able to construct for you. As for the error that you are reporting... put it in debug mode (2) and see what query is being passed through. I suspect that id is null and that is why all the records are being deleted. FWIW I have this in core.php which lets me turn debug on and off without pissing off my users. $skdebug = 0; //if ($_SERVER["REMOTE_ADDR"]== '118.92.79.110') $skdebug = 2; define('DEBUG', $skdebug); Code (markup): oh, and wrap [ php ] or [ code ] around your code snippets to make them more readable