Skip to content

Instantly share code, notes, and snippets.

@shahariaazam
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save shahariaazam/b4d7214ab189b76d7cb9 to your computer and use it in GitHub Desktop.

Select an option

Save shahariaazam/b4d7214ab189b76d7cb9 to your computer and use it in GitHub Desktop.
<?php
// model/Article.php
App::uses('AppModel', 'Model');
class Article extends AppModel
{
public $hasAndBelongsToMany = array(
'Category' =>
array(
'className' => 'Category',
'joinTable' => 'articles_categories',
'foreignKey' => 'article_id',
'associationForeignKey' => 'category_id',
'unique' => true
)
);
}
// controllers/ArticlesController.php
class ArticlesController extends AppController
{
public function test()
{
$articlesFromCatOne = $this->Article->find('all', array('conditions' => array('Category.id' => 1)));
var_dump($articlesFromCatOne); die();
}
}
// model/Category.php
// class Category extends AppModel
{
public $hasAndBelongsToMany = array(
'Article' =>
array(
'className' => 'Article',
'joinTable' => 'articles_categories',
'foreignKey' => 'category_id',
'associationForeignKey' => 'article_id',
'unique' => true
)
);
}
@shahariaazam
Copy link
Author

But when I visit ../articles/test then it shows the following error

But I wanted to show all articles from a specific category. So what's the wrong I can't get it. It may be silly but my brain is being fucked up! LOL

CakePHP: the rapid development php framework

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Category.id' in 'where clause'

Error: An Internal Error Has Occurred.

The details debug output given for deep investigation:

# Stack Trace
  • CORE/Cake/Model/Datasource/DboSource.php line 458 → PDOStatement->execute(array)
    array()
  • CORE/Cake/Model/Datasource/DboSource.php line 424 → DboSource->_execute(string, array)
    'SELECT Article.id, Article.title, Article.slug, Article.description, Article.status, Article.read, Article.modified, Article.created FROM dealerportal_kb.articles AS Article WHERE Category.id = 1'
    array()
  • CORE/Cake/Model/Datasource/DboSource.php line 665 → DboSource->execute(string, array, array)
    'SELECT Article.id, Article.title, Article.slug, Article.description, Article.status, Article.read, Article.modified, Article.created FROM dealerportal_kb.articles AS Article WHERE Category.id = 1'
    array()
    array()
  • CORE/Cake/Model/Datasource/DboSource.php line 1077 → DboSource->fetchAll(string, boolean)
    'SELECT Article.id, Article.title, Article.slug, Article.description, Article.status, Article.read, Article.modified, Article.created FROM dealerportal_kb.articles AS Article WHERE Category.id = 1'
    false
  • CORE/Cake/Model/Model.php line 2903 → DboSource->read(Article, array)
    object(Article) {
    hasAndBelongsToMany => array(
    [maximum depth reached]
    )
    recursive => (int) -1
    actsAs => array(
    [maximum depth reached]
    )
    useDbConfig => 'default'
    useTable => 'articles'
    id => false
    data => array([maximum depth reached])
    schemaName => 'dealerportal_kb'
    table => 'articles'
    primaryKey => 'id'
    validate => array([maximum depth reached])
    validationErrors => array([maximum depth reached])
    validationDomain => null
    tablePrefix => ''
    plugin => null
    name => 'Article'
    alias => 'Article'
    tableToModel => array(
    [maximum depth reached]
    )
    cacheQueries => false
    belongsTo => array([maximum depth reached])
    hasOne => array([maximum depth reached])
    hasMany => array([maximum depth reached])
    Behaviors => object(BehaviorCollection) {}
    whitelist => array([maximum depth reached])
    cacheSources => true
    findQueryType => 'all'
    order => null
    virtualFields => array([maximum depth reached])
    __backAssociation => array([maximum depth reached])
    __backInnerAssociation => array([maximum depth reached])
    __backOriginalAssociation => array([maximum depth reached])
    __backContainableAssociation => array([maximum depth reached])
    findMethods => array(
    [maximum depth reached]
    )
    Category => object(Category) {}
    [protected] _schema => array(
    [maximum depth reached]
    )
    [protected] _associationKeys => array(
    [maximum depth reached]
    )
    [protected] _associations => array(
    [maximum depth reached]
    )
    [protected] _insertID => null
    [protected] _sourceConfigured => true
    [protected] _eventManager => object(CakeEventManager) {}
    [protected] _validator => null
    }
    array(
    'conditions' => array(
    'Category.id' => (int) 1
    ),
    'fields' => null,
    'joins' => array(),
    'limit' => null,
    'offset' => null,
    'order' => array(
    (int) 0 => null
    ),
    'page' => (int) 1,
    'group' => null,
    'callbacks' => true
    )
  • CORE/Cake/Model/Model.php line 2875 → Model->_readDataSource(string, array)
    'all'
    array(
    'conditions' => array(
    'Category.id' => (int) 1
    ),
    'fields' => null,
    'joins' => array(),
    'limit' => null,
    'offset' => null,
    'order' => array(
    (int) 0 => null
    ),
    'page' => (int) 1,
    'group' => null,
    'callbacks' => true
    )
    • APP/Controller/ArticlesController.php line 7 → Model->find(string, array)
      'all'
      array(
      'conditions' => array(
      'Category.id' => (int) 1
      )
      )
      [internal function] → ArticlesController->test()
  • CORE/Cake/Controller/Controller.php line 490 → ReflectionMethod->invokeArgs(ArticlesController, array)
    object(ArticlesController) {
    components => array(
    [maximum depth reached]
    )
    uses => array(
    [maximum depth reached]
    )
    name => 'Articles'
    helpers => array([maximum depth reached])
    request => object(CakeRequest) {}
    response => object(CakeResponse) {}
    viewPath => 'Articles'
    layoutPath => null
    viewVars => array([maximum depth reached])
    view => 'test'
    layout => 'default'
    autoRender => true
    autoLayout => true
    Components => object(ComponentCollection) {}
    viewClass => 'View'
    View => null
    ext => '.ctp'
    plugin => null
    cacheAction => false
    passedArgs => array([maximum depth reached])
    scaffold => false
    methods => array(
    [maximum depth reached]
    )
    modelClass => 'Article'
    modelKey => 'article'
    validationErrors => null
    Session => object(SessionComponent) {}
    Auth => object(AuthComponent) {}
    Article => object(Article) {}
    [protected] _responseClass => 'CakeResponse'
    [protected] _mergeParent => 'AppController'
    [protected] _eventManager => object(CakeEventManager) {}
    }
    array()
  • CORE/Cake/Routing/Dispatcher.php line 185 → Controller->invokeAction(CakeRequest)
    object(CakeRequest) {
    params => array(
    [maximum depth reached]
    )
    data => array([maximum depth reached])
    query => array([maximum depth reached])
    url => 'articles/test'
    base => ''
    webroot => '/'
    here => '/articles/test'
    [protected] _detectors => array(
    [maximum depth reached]
    )
    [protected] _input => ''
    }
  • CORE/Cake/Routing/Dispatcher.php line 160 → Dispatcher->_invoke(ArticlesController, CakeRequest, CakeResponse)
    object(ArticlesController) {
    components => array(
    [maximum depth reached]
    )
    uses => array(
    [maximum depth reached]
    )
    name => 'Articles'
    helpers => array([maximum depth reached])
    request => object(CakeRequest) {}
    response => object(CakeResponse) {}
    viewPath => 'Articles'
    layoutPath => null
    viewVars => array([maximum depth reached])
    view => 'test'
    layout => 'default'
    autoRender => true
    autoLayout => true
    Components => object(ComponentCollection) {}
    viewClass => 'View'
    View => null
    ext => '.ctp'
    plugin => null
    cacheAction => false
    passedArgs => array([maximum depth reached])
    scaffold => false
    methods => array(
    [maximum depth reached]
    )
    modelClass => 'Article'
    modelKey => 'article'
    validationErrors => null
    Session => object(SessionComponent) {}
    Auth => object(AuthComponent) {}
    Article => object(Article) {}
    [protected] _responseClass => 'CakeResponse'
    [protected] _mergeParent => 'AppController'
    [protected] _eventManager => object(CakeEventManager) {}
    }
    object(CakeRequest) {
    params => array(
    [maximum depth reached]
    )
    data => array([maximum depth reached])
    query => array([maximum depth reached])
    url => 'articles/test'
    base => ''
    webroot => '/'
    here => '/articles/test'
    [protected] _detectors => array(
    [maximum depth reached]
    )
    [protected] _input => ''
    }
    object(CakeResponse) {
    [protected] _statusCodes => array(
    [maximum depth reached]
    )
    [protected] _mimeTypes => array(
    [maximum depth reached]
    )
    [protected] _protocol => 'HTTP/1.1'
    [protected] _status => (int) 200
    [protected] _contentType => 'text/html'
    [protected] _headers => array([maximum depth reached])
    [protected] _body => null
    [protected] _file => null
    [protected] _fileRange => null
    [protected] _charset => 'UTF-8'
    [protected] _cacheDirectives => array([maximum depth reached])
    [protected] _cookies => array([maximum depth reached])
    }
  • APP/webroot/index.php line 115 → Dispatcher->dispatch(CakeRequest, CakeResponse)
    object(CakeRequest) {
    params => array(
    [maximum depth reached]
    )
    data => array([maximum depth reached])
    query => array([maximum depth reached])
    url => 'articles/test'
    base => ''
    webroot => '/'
    here => '/articles/test'
    [protected] _detectors => array(
    [maximum depth reached]
    )
    [protected] _input => ''
    }
    object(CakeResponse) {
    [protected] _statusCodes => array(
    [maximum depth reached]
    )
    [protected] _mimeTypes => array(
    [maximum depth reached]
    )
    [protected] _protocol => 'HTTP/1.1'
    [protected] _status => (int) 200
    [protected] _contentType => 'text/html'
    [protected] _headers => array([maximum depth reached])
    [protected] _body => null
    [protected] _file => null
    [protected] _fileRange => null
    [protected] _charset => 'UTF-8'
    [protected] _cacheDirectives => array([maximum depth reached])
    [protected] _cookies => array([maximum depth reached])
    }
  • CakePHP: the rapid development php framework
    (accounts) 1 query took 1 ms
    Nr Query Error Affected Num. rows Took (ms)
    1 SELECT Session.id, Session.data, Session.expires FROM dealerportal_accounts.cake_sessions AS Session WHERE id = 'u080be1h6cbe31ao5j1mdve313' LIMIT 1 1 1 1

(default) 0 query took ms
Nr Query Error Affected Num. rows Took (ms)

MySQL Schema

--
-- Table structure for table `articles`
--

CREATE TABLE IF NOT EXISTS `articles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) CHARACTER SET utf8 NOT NULL,
  `slug` varchar(255) NOT NULL,
  `description` text CHARACTER SET utf8 NOT NULL,
  `status` int(1) DEFAULT '1' COMMENT '<1 = published> <2 = draft>',
  `read` int(8) NOT NULL DEFAULT '0',
  `modified` datetime NOT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

-- --------------------------------------------------------

--
-- Table structure for table `articles_categories`
--

CREATE TABLE IF NOT EXISTS `articles_categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category_id` int(11) DEFAULT NULL,
  `article_id` int(11) NOT NULL,
  `status` int(1) NOT NULL DEFAULT '1',
  `modified` datetime NOT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=56 ;

-- --------------------------------------------------------

--
-- Table structure for table `categories`
--

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `slug` varchar(50) NOT NULL,
  `status` int(1) DEFAULT '1',
  `modified` datetime NOT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment