przewiń do treści

Zend Framework od podstaw

Włodzimierz Gajda

function getPanstwaIds()
{
    $sql = 'select panstwo_id from panstwo where kontynent_id = ?';
    return $this->
                getTable()->
                getAdapter()->
                fetchCol($sql, $this['kontynent_id']);
}

function dodajPanstwa($ids)
{
    $panstwa = implode(' , ', $ids);
    $sql =
        'update panstwo set kontynent_id = '.
        $this->kontynent_id .
        ' where panstwo_id in ( ' .
        $panstwa .
        ' )';
    $db = $this->getTable()->getAdapter();
    $db->query($sql);
}

function usunPanstwa($ids)
{
    $panstwa = implode(' , ', $ids);
    $sql =
        'update panstwo set kontynent_id = null where panstwo_id in ( ' .
        $panstwa .
        ' )';
    $db = $this->getTable()->getAdapter();
    $db->query($sql);
}

function ustalPanstwa($ids)
{
    if (!is_array($ids)) {
        $ids = array();
    }

    $biezace = $this->getPanstwaIds();

    $do_usuniecia = array_diff($biezace, $ids);
    $do_dodania = array_diff($ids, $biezace);

    if (!empty($do_dodania)) {
        $this->dodajPanstwa($do_dodania);
    }

    if ($do_usuniecia) {
        $this->usunPanstwa($do_usuniecia);
    }
}

Listing 32.10. Dodatkowe metody potrzebne do przetwarzania formularza tabeli kontynent

Rozdział 32. Edycja zależności relacyjnych

listing-32-10.txt