· 8 years ago · Aug 22, 2018, 09:34 AM
1Data saving as blank in CakePHP
2foreach($interests as $id){
3 $this->data['UserInterest']['user_id'] = $user_id;
4 $this->data['UserInterest']['interest_id'] = $id;
5 $this->data['UserInterest']['other_interest'] = $other;
6 $UserInterest = new UserInterest;
7 if(!$UserInterest->save($this->data)) {
8 $this->Session->setFlash(__l('Failed to save Interests.') , 'default', null, 'error');
9 }
10 }
11
12$this->data['UserInterest']['other_interest'] = $other;
13echo $this->data['UserInterest']['other_interest'];
14 $UserInterest = new UserInterest;
15
16CREATE TABLE IF NOT EXISTS `users_interests` (
17 `id` bigint(20) NOT NULL AUTO_INCREMENT,
18 `user_id` bigint(20) NOT NULL,
19 `interest_id` bigint(20) NOT NULL,
20 `other_interest` varchar(150) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
21 PRIMARY KEY (`id`)
22)
23
24<?php
25class UserInterest extends AppModel
26{
27 var $name = 'UserInterest';
28
29 var $useTable="users_interests";
30
31 //$validate set in __construct for multi-language support
32 //The Associations below have been created with all possible keys, those that are not needed can be removed
33 var $belongsTo = array(
34 'User' => array(
35 'className' => 'User',
36 'foreignKey' => 'user_id',
37 'conditions' => '',
38 'fields' => '',
39 'order' => '',
40 ) ,
41 'Interests' => array(
42 'className' => 'Interest',
43 'foreignKey' => 'interest_id',
44 'conditions' => '',
45 'fields' => '',
46 'order' => '',
47 )
48 );
49 function __construct($id = false, $table = null, $ds = null)
50 {
51 parent::__construct($id, $table, $ds);
52 $this->validate = array(
53 'user_id' => array(
54 'rule' => 'numeric',
55 'allowEmpty' => false,
56 'message' => __l('Required')
57 )
58 );
59 }
60}
61?>
62
63$this->data['UserInterest']['user_id'] = $user_id;
64 $this->data['UserInterest']['interest_id'] = $id;
65 $this->data['UserInterest']['other_interest'] = $other;
66 echo '<pre>';
67 var_dump($this->data['UserInterest']);
68 var_dump($other);
69 var_dump($this->data['UserInterest']['other_interest']);
70
71 $this->UserInterest = ClassRegistry::init('UserInterest');
72
73array(3) {
74 ["user_id"]=>
75 string(3) "659"
76 ["interest_id"]=>
77 string(2) "10"
78 ["other_interest"]=>
79 string(17) "Share Investments"
80}
81string(17) "Share Investments"
82string(17) "Share Investments"
83
84$this->Modelname->create();