Display the page title dynamically - codeigniter

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.

Related

Create two tables in same schema.sql H2

I'm using Spring boot, and I have to initiate two tables for testing. I'm using the schema.sql inside the resource folder. But when I'm trying to create two tables in the same script and run the app, it can't load the application context.
Here is my schema.sql which I have placed at resource folder:
CREATE TABLE JobStatus_FO
(
id int(11) NOT NULL AUTO_INCREMENT,
businessDate timestamp NOT NULL,
label varchar(50),
);
CREATE TABLE JobStatusDetails_FO
{
id int(11) NOT NULL,
name varchar(50),
};
Please find the correct scripts as
CREATE TABLE JobStatus_FO
(
id INT(11) NOT NULL AUTO_INCREMENT,
businessDate TIMESTAMP NOT NULL,
label VARCHAR(50),
KEY id(id)
);
CREATE TABLE JobStatusDetails_FO
(
id INT(11) NOT NULL,
NAME VARCHAR(50)
);
Your syntax is not correct of create table .
1) In your scripts you have used extra comma "," before closing parentheses
2) Auto increment column should be used as a key in table
3) Curly braces "{" is not used within create table.
Hope this will work in to your project.

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?

codeigniter active record where_in

i'm using codeigniter 2xx.
Mysql table:
create table hobby (
id int,
school varchar,
classes varchar,
basketball text, {12,15,17...}
football text, {12,18,20...}
swimming text {11,15,....}
);
i intend to store the student id as serialized(array(integer)) in the mysql table fields like basketball, football and swimming.
I want to find out a particular class student id (eg. 12) if he has join any hobby or more than 1 hobby using codeigniter active records method but stuck. Below is my code:
$this->db->select('basketball','football','swimming');
$this->db->or_where('school', $data)->where('classes', $classid)->where_in($student_id, 'basketball');
$this->db->or_where('school', $data)->where('classes', $classid)->where_in($student_id, 'football');
$this->db->or_where('school', $data)->where('classes', $classid)->where_in($student_id, 'swimming');
$query = $this->db->get('hobby');
Or is there a better way to store & handle the information?
CREATE TABLE `student_hobby` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`student_id` INT(11) DEFAULT NULL,
`hobby_id` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
CREATE TABLE `hobby` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(500) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
set hobby student:
$this->db->insert('student_hobby', array('student_id' => 12, 'hobby_id' => 1));
select student who has 1 or more hobby:
$this->db->select('student.*, hobby.name');
$this->db->from('student_hobby');
$this->db->where('studend_id', 12);
$this->db->join('hobby', 'hobby.id = student_hobby.hobby_id');
// join to your existing student table. Fields `school` and `class` should be in `student` table.
$this->db->join('student', 'student.id = student_hobby.student_id');
$result = $this->db->get();
if($result->num_rows()) {
// if student has one or more hobbies
}

Add attributes to ActiveRecord from fieldTable

I have an Yii project, where I want to add fields on the profile page, which are described in another model.
So the table could look like this:
CREATE TABLE `object_field` (
`id` INT NOT NULL AUTO_INCREMENT ,
`varname` VARCHAR(255) NULL ,
`title` VARCHAR(255) NULL ,
`field_type` VARCHAR(255) NOT NULL ,
`match` VARCHAR(255) NULL ,
`range` VARCHAR(255) NULL ,
`default` VARCHAR(255) NULL ,
`required` INT(1) NOT NULL DEFAULT 0 ,
`position` INT(3) NOT NULL DEFAULT 0 ,
`visible` INT(1) NOT NULL DEFAULT 0 ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB
So in the UserProfile-model, I have added the fields to the rules() and attributesLabels() but the __get() and __set() does can't find the attributes before I add e.g. public $this->fieldName to model.
The values to the user is stored in another table, which connects the User with the UserData
Should I overwrite the __get() and __set() in the model, or how can I make my fields to the form accept using dynamic defined fields?
Update:
I have looked at the Yii-user extension, http://www.yiiframework.com/extension/yii-user/, but it does just change the UserProfile-schema to add the new fields. I could have 20 different types of users, and each of them shares some fields and maybe have some special fields just for the one type.
A solution could be to add the mongoDB as a relation. But I have never worked with that technology.

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

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;

Resources