CodeIgniter dependencies between models, without using any ORM or DataMapper systems - codeigniter

I'm writing a CRUD system using CodeIgniter, implementing some 'one to many' dependencies between models (without using any ORM or DataMapper systems) by calling methods from one model to another.
For example: in a "many Documents per User" realeation, when deleting a User, the User_Model directly calls the delete() method on the Document_Model, deleting any documents associated with it.
I’m certain there’s a better way implementing 'one to many' model releation (without ORM, etc.. ), and would appreciate some guidance.
Thanks
Alon.

Of course! You are allowed to use InnoDB engine. This engine has functionally foreign keys, so you can do smth like that, for example (very basic)
CREATE TABLE IF NOT EXISTS `documents` (
`did` bigint(11) NOT NULL AUTO_INCREMENT,
`uid` bigint(11) NOT NULL,
`data` varchar(255) NOT NULL,
PRIMARY KEY (`did`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `user` (
`uid` bigint(11) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`password` varchar(25) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `documents`
ADD CONSTRAINT `documents_cstr_1` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON DELETE NO ACTION ON UPDATE CASCADE;

Related

Display the page title dynamically

I've an existing CodeIgniter Application with lot of pages where all the page titles( h1 tag ) are hard coded in the views. Now I'm looking to populate the titles dynamically from the below table structure with as much less effort as possible.
CREATE TABLE `page_title` (
`id` int(11) NOT NULL,
`page_no` tinytext NOT NULL,
`page_title` varchar(255) NOT NULL,
`date` tinytext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `page_title`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
Need suggestion on how to display the title with a good approach. I was thinking to create a common model and call the model in the view with page_id pass to get the page_no and page_title for that id.

Laravel - Is it possible to make relation between models (belongsTo) via an element of array as text in table

I'm absolute beginner in Laravel.
Is it possible to make relation, say belongsTo between models via an element of array.
Column in table1 like this:
mapping: ["misc","rates","photos"]
I need something similar to following:
public function dependencies() : BelongsTo
{
// This is impossible because "mapping" is array in Depends model and text type in database.
return $this->belongsTo(Depends::class, 'mapping', 'element_of_array');
}
or something similar (hasMany etc).
Table looks like this:
CREATE TABLE `dependings` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`media_id` int(10) unsigned NOT NULL,
`type` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL,
`mapping` text COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `dependent_collection_mappings_collection_id_type_unique` (`media_id`,`type`),
CONSTRAINT `dependent_collection_mappings_collection_id_foreign` FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Laravel Eloquent relationship with more than one join column

I'm new to laravel and try to use Eloquent.
Assuming there is the following database structure:
CREATE TABLE `a` (
`userid` int(11) NOT NULL,
`aid` int(11) NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `p` (
`userid` int(11) NOT NULL,
`pid` int(11) NOT NULL,
`aid` int(11) NOT NULL,
`text` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `a` ADD PRIMARY KEY (`userid`,`aid`);
ALTER TABLE `p` ADD PRIMARY KEY (`userid`,`pid`);
Now I want a relation between the tables:
p contains a with p.userid=a.userid AND p.aid=a.aid
How can I do a relationship with more than one column?
First I want to mention that if you can't find a straight solution, there is always a workaround. The reason why you cannot do a relationship with more than one column in one table is that you need the same number of columns in the other table. So if you had 2 columns of type int in the first table, you need 2 columns of type int in the second table aswell to link them both. And as I see from what you provided, you are repeating data which suggests that your database is not normalized. I would suggest you do the following:
CREATE TABLE `a` (
`userid` int(11) NOT NULL,
`aid` int(11) NOT NULL unique,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `p` (
`id` int(11) NOT NULL,
`userid` int(11) NOT NULL,
`pid` int(11) NOT NULL unique,
`text` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `a` ADD PRIMARY KEY (`userid`);
ALTER TABLE `p` ADD PRIMARY KEY (`id`);
ALTER TABLE `p` ADD FOREIGN KEY (`userid`) REFERENCES `a` (`userid`)
This is what I think that is more suitable for you. However to answer your question, since you are using laravel in the a model do this:
class A extends Eloquent{
public function p(){
return $this->belongsTo('App\P','userid')
// or hasMany
}
public function p(){
return $this->belongsTo('App\P','aid')
// or hasMany
}
}
and the same thing in the p model
class P extends Eloquent{
public function a(){
return $this->hasMany('App\A','userid')
//or belongsTO
}
public function a(){
return $this->hasMany('App\A','pid')
//or belongsTO
}
}
NOTE
PLEASE keep in mind that the method belongsTo or hasMany is decided by your database model, on how you defined the database.

yii2 : can't get data from two table with active record

I want get data from two table comments,users.
comments have hasOne relation with users
public function getUser()
{
return $this->hasOne(Users::className(), ['user_id' => 'user_id']);
}
i want get comments.comment_id,comments.comment_content,comments.user_id from comments table and uses.user_name , users.user_display_name from users table to use in gridview widget.
i use
$res = Comments::find()
->select([
'comments.comment_id',
'comments.comment_content',
'comments.user_id',
'users.user_id',
'users.user_display_name',
'users.user_name',
])
->innerJoinWith('user')
->all();
this code get comments field but i can't get users.user_name,users.user_display_name from database.
How should I do it?
note: user table in database is users but when i create model with Gii,relation method declare as getUser(), i don't know why.
update 1:
comments table:
DROP TABLE IF EXISTS `comments`;
CREATE TABLE `comments` (
`comment_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`comment_content` text NOT NULL,
`comment_approved` enum('no','yes') NOT NULL DEFAULT 'no',
`comment_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`comment_parent` bigint(20) unsigned NOT NULL DEFAULT '0',
`sms_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`user_id` bigint(20) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`comment_id`),
KEY `fk_comment_sms_id` (`sms_id`),
KEY `fk_commetn_user_id` (`user_id`),
CONSTRAINT `fk_comment_sms_id` FOREIGN KEY (`sms_id`) REFERENCES `sms` (`sms_id`),
CONSTRAINT `fk_commetn_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
users table:
CREATE TABLE `users` (
`user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(60) NOT NULL DEFAULT '',
`user_pass` varchar(64) NOT NULL DEFAULT '',
`user_level` int(11) NOT NULL DEFAULT '1',
`user_email` varchar(100) NOT NULL DEFAULT '',
`user_display_name` varchar(250) NOT NULL DEFAULT '',
`user_phone_number` varchar(11) NOT NULL,
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_activation_key` varchar(60) NOT NULL,
`user_status` enum('active','deactive','delete') NOT NULL DEFAULT 'deactive',
PRIMARY KEY (`user_id`),
UNIQUE KEY `u_user_sign` (`user_name`) USING BTREE,
UNIQUE KEY `u_user_email` (`user_email`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
Not sure how you're displaying or testing if the query returned any data but you should read up on relations in Yii.
Working with Relational Data
Lazy and Eager Loading
With lazy loading the data doesn't exist until you try to access it so may not appear in things like print_r or var_dump
I recommend renaming your tables to comment and user. Read here: Table Naming Dilemma: Singular vs. Plural Names why you should use singular names.
The reason Gii generates getUser and not getUsers is because of the hasOne relationship. It's getting one -user- and not multiple -users-
Are you sure the relationship definition is correct? Is user_id the PK in the users table and the FK in the comments table? And are you sure there is correct data in the DB?

Table creation with h2 database

I am new to h2.I just using h2 in spring embedded mode with hibernate.I am trying to execute the following scripts using h2.
CREATE TABLE acct_authority (
id bigint(20) NOT NULL auto_increment,
name varchar(255) NOT NULL default '',
value varchar(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY name (name)
);
The table acct_authority is created without any error.But if i create another table with the following script.
CREATE TABLE acct_role (
id bigint(20) NOT NULL auto_increment,
name varchar(255) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY name (name)
);
It shows error as constraint name allready exists.What mistake i did.
You tried to create two constraints with same name. As you see, both CREATE TABLE statements contain following:
UNIQUE KEY name (name)
Result is that first one creates constraint named name, and second one fails because constraint name already exists. Problem can be solved by using unique names. Also in general it makes sense have little bit more descriptive names for database objects. Maybe you can use for example something like following:
UNIQUE KEY acct_authority_name_UNIQUE (name)
...
UNIQUE KEY acct_role_name_UNIQUE (name)

Resources