Data can't save in JoinTable

Discussion in 'PHP' started by mpstrategies, Feb 26, 2013.

  1. #1
    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:

     
    Last edited by a moderator: Feb 26, 2013
    mpstrategies, Feb 26, 2013 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    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.
     
    deathshadow, Feb 26, 2013 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,808
    Likes Received:
    4,535
    Best Answers:
    123
    Trophy Points:
    665
    #3
    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.
     
    sarahk, Feb 26, 2013 IP
  4. mpstrategies

    mpstrategies Active Member

    Messages:
    116
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    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:
     
    Last edited by a moderator: Feb 26, 2013
    mpstrategies, Feb 26, 2013 IP
  5. mpstrategies

    mpstrategies Active Member

    Messages:
    116
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #5
    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
     
    mpstrategies, Feb 26, 2013 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,808
    Likes Received:
    4,535
    Best Answers:
    123
    Trophy Points:
    665
    #6
    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 :)
     
    sarahk, Feb 26, 2013 IP