не могу получить модель 2-го уровня с cakephp - PullRequest
0 голосов
/ 04 февраля 2012

У меня проблема с отношением HABTM ... У меня есть две модели: Tablets habtm Условия.Когда я запрашиваю термин, я могу получить планшеты, но не термины, относящиеся к планшетам.Мой файл модели выглядит нормально, таблица в базе данных также, запрос в порядке, где мне искать проблему?!

Срок модели:

class Term extends AppModel {
var $name = 'Term';
var $displayField = 'term';
var $actsAs = array('Searchable','ExtendAssociations','Tree', 'Containable');
var $validate = array(

    'term' => array(
        'rule' => 'notEmpty'
    )

);
var $belongsTo = array(
    'WordType' => array(
        'className' => 'WordType',
        'foreignKey' => 'word_type_id'

    ),
    'ParentTerm' =>
        array(  'className' => 'Term',
                'foreignKey' => 'parent_id'
    )


);

var $hasAndBelongsToMany = array(
    'Tablet' =>
        array(
            'className'              => 'Tablet',
            'joinTable'              => 'tablets_terms',
            'foreignKey'             => 'term_id',
            'associationForeignKey'  => 'tablet_id',
            'unique'                 => true,
            )
);


}

Модель планшета:

class Tablet extends AppModel {
var $name = 'Tablet';
var $displayField = 'no_museum';
var $actsAs = array ('Searchable','ExtendAssociations', 'Containable'); 

var $belongsTo = array( 'DateDay','DateMonth','Collection', 'Period', 'ArchLoc', 'ArchSite', 'Genre','Month', 'Year','Project', 'Collection','Ruler', 'MainVerb', 'Official','MainAction'); 

var $hasMany = array('TabletFile');


var $hasAndBelongsToMany = array(
    'Tag' =>
        array(
            'className'              => 'Tag',
            'joinTable'              => 'tablets_tags',
            'foreignKey'             => 'tablet_id',
            'associationForeignKey'  => 'tag_id',
            'unique'                 => true,
            ),
    'FromPerson' =>
        array(
            'className'              => 'FromPerson',
            'joinTable'              => 'from_people_tablets',
            'foreignKey'             => 'tablet_id',
            'associationForeignKey'  => 'term_id',
            'unique'                 => true,
            ),
    'ToPerson' =>
        array(
            'className'              => 'ToPerson',
            'joinTable'              => 'tablets_to_people',
            'foreignKey'             => 'tablet_id',
            'associationForeignKey'  => 'term_id',
            'unique'                 => true,
            ),
    'FromLocation' =>
        array(
            'className'              => 'FromLocation',
            'joinTable'              => 'from_locations_tablets',
            'foreignKey'             => 'tablet_id',
            'associationForeignKey'  => 'term_id',
            'unique'                 => true,
            ),
    'ToLocation' =>
        array(
            'className'              => 'ToLocation',
            'joinTable'              => 'tablets_to_locations',
            'foreignKey'             => 'tablet_id',
            'associationForeignKey'  => 'term_id',
            'unique'                 => true,
            ),
    'Language' =>
        array(
            'className'              => 'Language',
            'joinTable'              => 'languages_tablets',
            'foreignKey'             => 'tablet_id',
            'associationForeignKey'  => 'language_id',
            'unique'                 => true,
            ),
    'Group' =>
        array(
            'className'              => 'Group',
            'joinTable'              => 'groups_tablets',
            'foreignKey'             => 'tablet_id',
            'associationForeignKey'  => 'group_id',
            'unique'                 => true,
            ),
    'Term' =>
        array(
            'className'              => 'Term',
            'joinTable'              => 'tablets_terms',
            'foreignKey'             => 'tablet_id',
            'associationForeignKey'  => 'term_id',
            'unique'                 => true,
            ),

    'Keyword' =>
        array(
            'className'              => 'Keyword',
            'joinTable'              => 'keywords_tablets',
            'foreignKey'             => 'tablet_id',
            'associationForeignKey'  => 'keyword_id',
            'unique'                 => true,
            )

        );  

function beforeSave() {
 $termIds = $this->_getTermIds();

     if(!empty($termIds)) {
         if(!isset($this->data['Term']['Term'])) {
             $this->data['Term']['Term'] = $termIds;
         } else {
            foreach($termIds as $termId) {
                 $this->data['Term']['Term'][] = $termId;
            }
         }
     }

//debug($this);
     return true;
 }

function _getTermIds() {

$terms=$this->data['Tablet']['translit'];
$terms= str_replace(array('\r\n', '\r', '\n','\n\r','\t'), ' ', $terms);
$terms = trim($terms, chr(173));
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
$terms = mb_ereg_replace('\s+', ' ', $terms);
$terms = explode(" ", $terms);
$terms=array_map('trim', $terms);
$anti_terms = array('@tablet','1.','2.','3.','4.','5.','6.','7.','7.','9.','10.','11.','12.','13.','14.','15.','16.','17.','18.','19.','20.','1\'.','2\'.','3\'.','4\'.','5\'.','Rev.',
'Obv.','@tablet','@obverse','@reverse','S1','S2','C1','C2','C3','C4','C5','C6','C7','C8','C9', '\r', '\n','\r\n', '\t',''. ' ', null, chr(173), 'x', '[x]','[...]' );
foreach($terms as $key => $term) {
    if(in_array($term, $anti_terms) || is_numeric($term)) {
        unset($terms[$key]);
        }
    }

 if(Set::filter($terms)) {
    foreach($terms as $term) {
    $this->Tablet->Term = $this->Term;
    $existingTerm = $this->Tablet->Term->find('first', array('conditions' => array('Term.term' => $term)));

         if(!$existingTerm) {
             $this->Term->create();
             $this->Term->saveField('term', $term);

             $termIds[] = $this->Term->id;
         }
         else {
             $termIds[] = $existingTerm['Term']['id'];
         }
     }

     return array_unique($termIds);
 }

 return false;
 }

}

Term controller

function view($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid term', true));
        $this->redirect(array('action' => 'index'));
    }

    $term=$this->Term->find( 'first', array( 'conditions' => array('Term.id' => $id),/* 'recursive'=> 2 */ 'contain'=>array( 'WordType', 'Tablet' => array('Term') )));

    $this->set('term', $term);
}

В представлении:

<?php debug($term);?>

результат:

    Array
(
[Term] => Array
    (
        [id] => 626
        [word_type_id] => 7
        [term] => [ubi.]ku6
        [sumerograms] => 
        [translation] => 
        [akkadian] => 
        [comments] => 
        [bibliography] => 
        [parent_id] => 0
        [lft] => 446
        [rght] => 447
    )

[WordType] => Array
    (
        [id] => 7
        [word_type] => unknown
        [parent_id] => 0
    )

[Tablet] => Array
    (
        [0] => Array
            (
                [id] => 97
                [arch_loc_id] => 
                [arch_site_id] => 3
                [period_id] => 1
                [genre_id] => 2
                [month_id] => 1
                [year_id] => 3
                [collection_id] => 14
                [no_arch] => 
                [no_museum] => AO
                [no_cdli] => P220929
                [no_etcsl] => 
                [no_perso] => DP 279
                [size] => 
                [date_day] => 0
                [date_month] => 0
                [subject] => livraison de poissons par de nombreux pêcheurs pour Baranamtara
                [translit] => Obv.
C1
1. 3 u tar.ku6
2. 2 geš’u 6 geš2 [ubi.]ku6
3. [1] [geš’u] [gir.ku6] [mun]
4. [1] [geš2] [3] [u] [ba]
5. [sila3] [i3-ku6]
etc etc bla bla long text.
                [translation] => 
                [abstract] => 
                [epi_notes] => 
                [comments] => Pour un festival !
                [publications] => DP 279, Allotte de la Fuÿe, François M., 1912
                [project_id] => 2
                [ruler_id] => 61
                [main_verb_id] => 345
                [official_id] => 165
                [main_action_id] => 9
                [date_day_id] => 
                [date_month_id] => 
                [TabletsTerm] => Array
                    (
                        [id] => 19482
                        [tablet_id] => 97
                        [term_id] => 626
                    )

            )

    )

)

обратите внимание, что cakephp создал модель TabletsTermэто не TabletsTerms, это кий?

В любом случае, я просто не знаю, где искать.Есть предложения?

Спасибо!

...