CakePHP - Fetching data, checking against TRUE/FALSE - mysql

In my database, my table has the following structure:
CREATE TABLE `tbl_app_versions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`app` varchar(100) DEFAULT NULL,
`version` varchar(50) DEFAULT NULL,
`force_update` bit(1) DEFAULT NULL,
`is_active` bit(1) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
Note that 'force_update' is bit. It can be either 1 or 0.
In my code, I fetch the data and want to see if the value is 1 or 0. CakePHP for some reason returns the following in either case:
Array
(
[0] => Array
(
[Platforms] => Array
(
[id] => 1
[app] => ios
[version] => 1.0.042
[force_update] =>
[is_active] =>
[created] =>
[modified] =>
)
)
[1] => Array
(
[Platforms] => Array
(
[id] => 2
[app] => android
[version] => 1.0.041
[force_update] =>
[is_active] =>
[created] =>
[modified] =>
)
)
)
Regardless of it's value, force_update shows the same. I'm using bit in most of my tables and I have to be able to do this check.
This:
<?php echo ($value['Platforms']['force_update']) ? 'Yes' : 'No'; ?>
Or:
<?php echo ($value['Platforms']['force_update'] = TRUE) ? 'Yes' : 'No'; ?>
Or:
<?php echo ($value['Platforms']['force_update'] == TRUE) ? 'Yes' : 'No'; ?>
all result in the same despite the fact that my data is:
'1', 'ios', '1.0.042', '0', '1', NULL, NULL
'2', 'android', '1.0.041', '1', '1', NULL, NULL

When using bit in database, if field is NULL, it returns FALSE, otherwise it returns TRUE.
So if it has any value in the field (1 or 0) it still returns TRUE.
Solution:
Change the field from bit to tinyint.
OR
On data manipulation, NULL the field in terms of FALSE and give it a value in terms of TRUE. This way every time I read the data it's either NULL or has a value, then based on that I can show the proper message in the views.

You could switch from bit to tinyint (1) but I think your if statement is wrong.
Try
<?php echo (!empty($value['Platforms']['force_update'])) ? 'Yes' : 'No'; ?>
empty() will catch botch NULL and 0 as 'No' and only 1 as 'yes'.

Related

#mysql/xdevapi returning invalid input expression when inserting data (MySQL 8)

I'm using the #mysql/xdevapi package with NodeJS and the MySQL 8 docker container.
The error I'm getting is:
Error: invalid input expression
at module.exports (C:\Users\BugHunter\Projects\nines\server\node_modules\#mysql\xdevapi\lib\DevAPI\Util\parseFlexibleParamList.js:43:15)
Here's my table:
CREATE TABLE IF NOT EXISTS `surveyrewards`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(20) NOT NULL,
`surname` VARCHAR(20) NOT NULL,
`birthday` DATE NULL,
`gender` ENUM("male", "female") NOT NULL,
`email` VARCHAR(255) NOT NULL,
`postcode` VARCHAR(10) NULL,
`is_subscribed` TINYINT NOT NULL DEFAULT 0,
`is_confirmed` TINYINT NOT NULL DEFAULT 0,
`last_mailed` TIMESTAMP NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC) VISIBLE)
ENGINE = InnoDB;
And my node code using these docs:
require('dotenv').config()
const mysqlx = require('#mysql/xdevapi')
const config = {
host: process.env.MYSQL_HOST ,
user: process.env.MYSQL_USER,
port: parseInt(process.env.MYSQL_PORT),
password: process.env.MYSQL_PASSWORD,
schema: process.env.MYSQL_DATABASE
}
mysqlx.getSession(config)
.then(session => {
console.log(process.env)
const table = session.getSchema(config.schema).getTable('users')
return table
.insert([ 'firstname', 'surname', 'email', 'gender', 'is_subscribed', 'is_confirmed' ])
.values('Bob', 'hope', 'bob#example.com', 'male', 0, 0)
.execute()
})
.then(() => console.log('Works'))
.catch(err => console.warn(err))
The error is vauge and I'm not sure how to progress from here as my code is the same. Any advice?
That is actually a bug, which is why the error message is not clear. :)
Falsy values such as 0 are not currently accepted as valid values() arguments. This should not be the case though. If you feel like it, you can report it via the MySQL bug tracker using the Connector for Node.js category, but you can consider this as being tracked.
One sort of nasty workaround is to use strings instead. Even if the SQL datatype is TINYINT like in your case, it will still work and the value will be "coerced".
return table
.insert([ 'firstname', 'surname', 'email', 'gender', 'is_subscribed', 'is_confirmed' ])
.values('Bob', 'hope', 'bob#example.com', 'male', '0', '0')
.execute()
Disclaimer: I'm the lead dev of the connector.

Accessing associated models of HABTM relationships via bindModel() w/o recursion

The Problem In A Nutshell
I want to retrieve data from Model A that HABTM Model B via a find() operation in Model B's controller without relying on extensive recursion.
$this->ModelB->bindModel('hasMany' => array('ModelAsModelBs'));
$this->ModelB->find('all', array('fields' => array('ModelA.*'))); //cond'ts below
I'm aware that bindModel() is required to do this, but I can't seem to get access to the associated model fields (ie. not just the HABTM table's fields, but the actual associated model) without multiple recursion.
It occurs to me that I may also be fundamentally misunderstanding something about how model relationships are supposed to interact, or be designed, or retrieved, etc.—in short, I recognize that the reason I may not be succeeding is that this may not be something I should be doing, henh. If this is so, I'd be equally happy learning how to do this better, because I frequently deal with very elaborate model relationships (I mostly do web development for academia and on-line course material/remote research).
Concrete Example / Actual Circumstances
The database has these tables, with id's as you'd expect:
Users
Courses
Modules
UsersCourses
UsersModules
CoursesModules
The model query I am trying execute is happening within the Users controller, and looks like this:
class Users extends AppController {
public function doSomething() {
$hasOne = array( 'hasOne' => array('CoursesModules','UsersModules'));
$this->User->Course->Module->bindModel($hasOne);
$conditions = array('`CoursesModules`.`course_id`' => $courseIds, //this is a known constraint within the app
'NOT' => array('`UsersModules`.`module_id` = `CoursesModules`.`module_id`' ));
$options = array( 'fields' => array('Module', 'Course.*'), // Note the attempt to get Course model information
'conditions' => $conditions,
'recursive' => 0);
$modules = $this->User->Course->Module->find('all', $options);
$this->set(compact('modules'));
}
}
This query results in:
Error: SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'Course'
But neither can I use bindModel() to connect Courses to Modules. This strikes me as strange since the association path is Users->Courses->Modules. I can bring recursion up a notch, but it causes all sorts of hell that requires a lot of unbind() and also pulls a completely absurd amount of data.
A second oddity is that if I remove the Course.* from the fields list, the above query executes but doesn't work as I'd expect; I think this is correctly asking for all Modules listed in CoursesModules that are not also in UsersModules. Such data does exist in my records, yet isn't retrieved by this.
I realize I can get course_ids from the CoursesModules and then just do another find to get the Course model data, but that's a) not very Cake-like and b) a pain because I'd really appreciate having access to $modules['Module']['Course'] in the rendered view file.
Can anyone point me in the right direction here? Or, haha, god forbid, help me just build this MySQL query (I am all thumbs with MySQL joins)? Truly appreciated, thanks!
UPDATE
#Kai: To set up the relationships I set up my tables and baked'em. Worth noting, perhaps, is that I have a fairly basic grasp of MySQL and generally do everything through PhpMyAdmin. As for generating the initial Cake files, I use cake bake all and then modified things as I went. The SQL for the tables and the $hasAndBelongsToMany arrays from the respective models are posted at the end.
As to why I chose hasOne... I also assumed hasMany; using this relationship consistly generated 'column not found' errors from the tables I was binding (didn't matter which column). Meanwhile, the obviously wrong choice of hasOne worked, to some extent.
And finally, I have had a lurking suspicion that this containable behavior business might be what I was after, but I don't really understand it. As briefly as I can, this is the context for these models and the sorts of queries I'm trying to execute:
I'm building a program for a university faculty that will basically let profs have some online coursework. But the coursework (ie. modules) might be shared between different classes (ie. courses), and students might be in any or all classes. An additional constraint is that a student may have a choice of which modules she'll do in a given course—the prof may offer five of which they'll have to complete any three. So, when a student logs in, I need to be able to retrieve the modules they haven't completed yet, in the context of the courses they're in.
There are a plethora of similar queries I've got to make that are more or less of this nature. As it stands, I can achieve all this (and since this is on a deadline, have done so) through various uses of loadModel(), executing a simpler $this->Model->find(), sorting the result through some foreach logic, rinse repeat. Aside from being irritating, I'm also worried it's not scalable because of undue processing on my part, and finally... I hate doing things wrong, haha. I know that CakePHP can handle the questions I want to ask of my data, I just don't know how to ask them (and/or set up the data so such questions can be asked).
CREATE TABLE IF NOT EXISTS `modules` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`domain_id` int(11) NOT NULL,
`subject_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`description` text NOT NULL,
`passing_score` int(11) NOT NULL,
`max_attempts` int(11) NOT NULL,
`allow_retry` int(11) NOT NULL,
`running_score` tinyint(1) NOT NULL,
`score_privacy` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `domain_id` (`domain_id`),
KEY `subject_id` (`subject_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`group_id` int(11) NOT NULL,
`institution_id` int(11) NOT NULL,
`password` varchar(255) NOT NULL,
`firstname` varchar(63) NOT NULL,
`lastname` varchar(63) NOT NULL,
`email` varchar(255) NOT NULL,
`studentno` varchar(31) NOT NULL,
`claimed` tinyint(1) NOT NULL,
`verified` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `group_id` (`group_id`),
KEY `institution_id` (`institution_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `courses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`institution_id` int(11) NOT NULL,
`coursecode` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`semester` varchar(7) NOT NULL,
`educator_id` int(11) NOT NULL,
`year` year(4) NOT NULL,
`description` text NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`educator_id`), /* educator is an alias of user in some cases */
KEY `institution_id` (`institution_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `users_courses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`,`course_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `users_modules` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`module_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`,`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE IF NOT EXISTS `courses_modules` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`course_id` int(11) NOT NULL,
`module_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `course_id` (`course_id`,`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Model Associations
Note: this is not comprehensive—hasOne,hasMany,belongsTo,etc. have been omitted so as to save on space; if need be I can post the entire model.
// from User Model
public $hasAndBelongsToMany = array(
'Course' => array(
'className' => 'Course',
'joinTable' => 'users_courses',
'foreignKey' => 'user_id',
'associationForeignKey' => 'course_id',
'unique' => 'keepExisting',
),
'Module' => array(
'className' => 'Module',
'joinTable' => 'users_modules',
'foreignKey' => 'user_id',
'associationForeignKey' => 'module_id',
'unique' => 'keepExisting',
)
);
// from Course Model
public $hasAndBelongsToMany = array(
'Module' => array(
'className' => 'Module',
'joinTable' => 'courses_modules',
'foreignKey' => 'course_id',
'associationForeignKey' => 'module_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
),
'User' => array(
'className' => 'User',
'joinTable' => 'users_courses',
'foreignKey' => 'course_id',
'associationForeignKey' => 'user_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
// from Module Model
public $hasAndBelongsToMany = array(
'Excerpt' => array(
'className' => 'Excerpt',
'joinTable' => 'excerpts_modules',
'foreignKey' => 'module_id',
'associationForeignKey' => 'excerpt_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
),
'Course' => array(
'className' => 'Course',
'joinTable' => 'courses_modules',
'foreignKey' => 'module_id',
'associationForeignKey' => 'course_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
As I understand it, this is the problem; A user has a list of available modules through their courses. We want to find available modules that the user has not completed. In other words, we have to find a user's course's modules which are not in that user's modules.
Here's the best way I can think of doing that:
$modules = $this->User->Module->find('all', array(
'joins' => array(
array(
'table' => 'courses_modules',
'alias' => 'CoursesModule',
'type' => 'LEFT',
'conditions' => array('CoursesModule.module_id = Module.id'),
),
array(
'table' => 'courses_users',
'alias' => 'CoursesUser',
'type' => 'LEFT',
'conditions' => array('CoursesUser.course_id = CoursesModule.course_id'),
),
array(
'table' => 'modules_users',
'alias' => 'ModulesUser',
'type' => 'LEFT',
'conditions' => array(
'ModulesUser.module_id = Module.id',
'ModulesUser.user_id' => $userId,
),
),
),
'conditions' => array(
'CoursesUser.user_id' => $userId,
'ModulesUser.id' => null,
),
'recursive' => -1,
));
Which should build a query like this:
SELECT Module.* FROM modules AS Module
LEFT JOIN courses_modules AS CoursesModule
ON (CoursesModule.module_id = Module.id)
LEFT JOIN courses_users AS CoursesUser
ON (CoursesUser.course_id = CoursesModule.course_id)
LEFT JOIN modules_users AS ModulesUser
ON (ModulesUser.module_id = Module.id
AND ModulesUser.user_id = $userId)
WHERE CoursesUser.user_id = $userId
AND ModulesUser.id IS NULL
The first two joins along with the user_id condition will get all the available modules. Rather than getting the user, their courses and then that course's modules we work backwards (kind of); we get the modules and join the courses to which we join the users. Once we add the user_id condition the association will be filtered and only modules that can be joined to the user will be found. Since we are starting with the modules there won't be any duplicates, we don't know (or care) which course was used to make the link.
With those available modules we then join the modules a user has completed and limit the records to those that couldn't be joined.
The CakePHP documentation has more info on joining tables.
Edit :
If you want to set the course IDs manually, you can do this:
$modules = $this->User->Module->find('all', array(
'joins' => array(
array(
'table' => 'courses_modules',
'alias' => 'CoursesModule',
'type' => 'LEFT',
'conditions' => array('CoursesModule.module_id = Module.id'),
),
array(
'table' => 'modules_users',
'alias' => 'ModulesUser',
'type' => 'LEFT',
'conditions' => array(
'ModulesUser.module_id = Module.id',
'ModulesUser.user_id' => $userId,
),
),
),
'conditions' => array(
'CoursesModule.course_id' => $courseIds,
'ModulesUser.id' => null,
),
'recursive' => -1,
));
There's something wrong with the way you set up your relationships. Could you post to show what you've done to set up the relationships, plus what the SQL query is that's giving the error? I'm also confused about what exactly your query is supposed to do.
I can make a few observations already though:
Course does not seem to be defined in your query, which is why you're getting a sql error. Probably there is something wrong with your relationships regarding Course.
If you're trying to do a HABTM relationship between Users and Modules and Courses and Modules, why are you doing creating a hasOne relationship between Module and CoursesModules and Module and UsersModules?
The short version of what I'm wondering is, shouldn't that be a hasMany instead of a hasOne?
The long version is, occasionally, when you have a join table like that, you'll need to for some reason create a relationship with the join table itself rather than just using HABTM (where when you get the data, the data in the join table is not included). Generally this is because there is data besides the two foreign keys that must also be stored in the join table. But in that case, you should be using what is known as a has many through relationship. If it's not necessary to use a has many through, you should just be using HABTM with users and courses, and not be creating relationships with directly with the join tables at all. http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model
Conditions for related models need to be in the contain options. Keep in mind that the normal behavior of Cake is to get related model data in additional queries, rather than doing a join and getting it all in one query. http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

MySQL Decimal empty string / null (Meekrodb)

I have the following table:
CREATE TABLE `Plot` (
`idPlot` int(11) NOT NULL,
`ListPrice` decimal(9,2) DEFAULT NULL,
`WebPrice` decimal(9,2) DEFAULT NULL,
`BottomPrice` decimal(9,2) DEFAULT NULL,
PRIMARY KEY (`idPlot`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to have values stored an NULL in the decimal fields if an empty string is passed. I can only seem to store 0.00 however.
I'm using Meekrodb to do the update:
$db->update('Plot', array(
'ListPrice' => $one['ListPrice'],
'BottomPrice' => $one['BottomPrice'],
'WebPrice' => $one['WebPrice']
), "idPlot=%s", $one['idPlot']);
My input array looks like this:
Array
(
[idPlot] => 6
[ListPrice] => 99,999.00
[BottomPrice] =>
[WebPrice] =>
)
Meekro runs:
UPDATE `Plot` SET `ListPrice`='99999.00', `BottomPrice`='', `WebPrice`='' WHERE idPlot='6'
And I get:
Array
(
[0] => Array
(
[idPlot] => 6
[ListPrice] => 99999.00
[WebPrice] => 0.00
[BottomPrice] => 0.00
)
)
stored in the database.
Is there any way of making it populate the fields with NULL rather than 0.00???
Thanks
Please note the difference between an empty string:
$foo = '';
... and a NULL value:
$foo = NULL;
The print_r() function does not display the difference; you need to use var_dump() to accurately dump your variables:
$data = array('', NULL);
print_r($data);
var_dump($data);
Array
(
[0] =>
[1] =>
)
array(2) {
[0]=>
string(0) ""
[1]=>
NULL
}
I believe you have to insert the value as NULL without quotations

SQLSTATE[HY000]: General error: 1366 Incorrect integer value:

$order = new Application_Model_DbTable_Order();
$orderno = $order->select()
->from($order, 'orderno')
->where('memberid = ?', $userid)
->order('orderno DESC')
->limit(1, 0);
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'SELECT ordertable.orderno FROM ordertable WHERE (memberid = '30') ORDER BY orderno DESC LIMIT 1' for column 'orderno' at row 1
Got this error and am wondering if there is anything wrong with my code, because I have searched everywhere for the cause but don't seem to find any help.
#SQL code for Ordertable#
`orderno` int(5) NOT NULL AUTO_INCREMENT,
`memberid` int(5) DEFAULT NULL,
PRIMARY KEY (`orderno`)
#SQL code for Item#
`itemid` int(5) NOT NULL AUTO_INCREMENT,
`image` varchar(100) NOT NULL,
`itemname` varchar(30) DEFAULT NULL,
`description` varchar(100) DEFAULT NULL,
`itemtype` varchar(20) DEFAULT NULL,
PRIMARY KEY (`itemid`)
#SQL code for Orderdetail#
`orderdetailno` int(5) NOT NULL AUTO_INCREMENT,
`orderno` int(5) NOT NULL,
`itemid` int(5) NOT NULL,
`unitcost` decimal(6,2) DEFAULT NULL,
PRIMARY KEY (`orderdetailno`),
KEY `orderno` (`orderno`),
KEY `itemid` (`itemid`)
This is my sql code if it helps I'm using MySQL.
$request = new Zend_Session_Namespace('cart');
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$userid = $user->userid;
$order = new Application_Model_DbTable_Order();
$itemdb = new Application_Model_DbTable_Item();
$orderdetail = new Application_Model_DbTable_Orderdetail();
$data = array ('memberid' => $userid);
$order->insert($data);
$orderno = $order->select()
->from($order, 'orderno')
->where('memberid = ?', $userid)
->order('orderno DESC')
->limit(1, 0);
foreach ($request->array as $var)
{
$unitprice = $itemdb->select()
->from('$itemdb', 'unitcost')
->where('itemid = ?', $var);
$newArray = array('orderno' => $orderno,
'itemid' => $var,
'unitcost' => $unitprice, );
$orderdetail->insert($newArray);
}
Any guidance will be very much appreciated.
Per https://stackoverflow.com/a/8882396/1432614, run
SELECT ##GLOBAL.sql_mode;
SELECT ##SESSION.sql_mode;
and if either include STRICT_TRANS_TABLES, remove it from the setting.
For example:
SET ##GLOBAL.sql_mode= 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
The reason you are getting that error is because you are attempting to use a Zend_Db_Select object as a value in your INSERT statement.
$orderno = $order->select()
->from($order, 'orderno')
->where('memberid = ?', $userid)
->order('orderno DESC')
->limit(1, 0);
And then inside the foreach loop:
$newArray = array('orderno' => $orderno, // <-- this becomes a SELECT statment
'itemid' => $var,
'unitcost' => $unitprice, );
$orderdetail->insert($newArray); // attempting to insert a select statement
You should execute the $orderno statement and fetch() the result if you want to use it in an insert statement:
$ordernum = $orderno->query()->fetch();
$newArray = array('orderno' => $ordernum,
'itemid' => $var,
'unitcost' => $unitprice, );
$orderDetail->insert($newArray);

cakephp messaging system scheme/model/controller Best Practice?

I am trying to implement a simple messaging system for my users.
I am not an habtm expert, so any assistance will be appreciated!
Here is what I got this far, please make any suggestions!! (cake1.3)
CREATE TABLE IF NOT EXISTS `app_messages` (
`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
`from_user_id` int(8) unsigned DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`body` text,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `app_messages_users` (
`id` int(8) unsigned NOT NULL,
`message_id` int(8) unsigned NOT NULL,
`to_user_id` int(8) unsigned NOT NULL,
`read` tinyint(1) DEFAULT '0',
`replied` tinyint(1) NOT NULL DEFAULT '0',
`trash` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
Message.php
var $hasAndBelongsToMany = array(
'User' =>
array(
'className' => 'User',
'joinTable' => 'messages_users',
'foreignKey' => 'message_id',
'associationForeignKey' => 'to_user_id',
'unique' => true
)
);
User.php
var $hasMany = array(
'Message' => array(
'className' => 'message',
'foreignKey' => 'from_user_id',
'dependent' => true
)
So now, my question is, Am I doing this correct?
How do I go around creating a SEND MESSAGE function, to insert correct values to both tables?
I am a total noob regarding HABTM relationships, but I am trying to learn. Have spent several hours reading about it on the web, but still, need to ask if I am going the right way.
Thanks for your time!!
-Tom
Given that your join table has additional fields (read, replied, trash), you should not use an HABTM relationship. If you do, you won't be able to access them from your application. So instead, you should configure two hasMany associations through a new model for the join table. It is well explained here in the CookBook.
So you'll end up having something like this:
// User.php
class User extends AppModel {
public $hasMany = array(
'MessageUser' /* you can call this 'SentMessage' or something that makes more sense */
);
}
// Message.php
class Message extends AppModel {
public $hasMany = array(
'MessageUser'
);
}
// MessageUser.php
class MessageUser extends AppModel {
public $belongsTo = array(
'User', 'Message'
);
}