set ID value for another field - php

Can I set the model record ID for another field value? (without update it)
In the other way, mix this code to one or two line:
$this->Model->create($data);
$this->Model->save();
$this->Model->create($data); // create again
$this->Model->set('link', $this->Model->id); // get record id
$this->Model->save(); // update
Thanks.

Yes, but you need to make the proper calls to the proper model methods. You will also need to make certain you are storing the information correctly in the array you are passing to save. So let's say you are adding a record to the places table. Let's say your schema looks like this:
CREATE TABLE 'places' (
`id` CHAR(36) NOT NULL,
`reference_id` CHAR(36) NULL,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
);
Then you have the following:
$data['Place']['name'] = 'New York';
// create the first record to get the PK ID that will
// be used in the reference_id of the next record
$this->Place->create();
$this->Place->save($data);
// set the reference ID to the ID of the previously saved record
$data['Place']['reference_id'] = $this->Place->id;
// create the next record recording the previous ID as
// the reference_id
$this->Place->create();
$this->Place->save($data);

Related

In a 1-1 relationship, why is my insert inserting two records in two tables?

I'm having trouble, as title says, when I INSERT a record in a table that has got a 1-1 relationship with another.
First things first, the SQL code that generates the tables:
DROP TABLE IF EXISTS Facebook_Info;
DROP TABLE IF EXISTS Conversations;
CREATE TABLE IF NOT EXISTS Conversations(
c_id INT AUTO_INCREMENT NOT NULL,
c_start TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
channel ENUM('desktop', 'facebook'),
u_name VARCHAR(20) DEFAULT NULL,
u_email VARCHAR(50) DEFAULT NULL,
PRIMARY KEY(c_id)
);
CREATE TABLE IF NOT EXISTS Facebook_Info (
c_id INT AUTO_INCREMENT NOT NULL,
f_id INT(12) NOT NULL,
PRIMARY KEY(c_id),
FOREIGN KEY(c_id) REFERENCES Conversations(c_id)
);
I assure you this code works: I tested it. I hope this is the best way to provide a 1-1 relationship between Conversations and Facebook_Info.
In any case, now I can introduce you my nightmare: I'm trying to insert a new record in Conversations via PHP (procedural style).
public function create_new_id_conv($channel = 1) {
$w_ch = '';
if ($channel == 2) {
$w_ch = 'facebook';
} else {
$w_ch = 'desktop';
}
$query = "INSERT INTO Conversations (c_id, c_start, channel) VALUES (NULL, CURRENT_TIMESTAMP,'$w_ch')";
$conn = mysqli_connect("localhost", Wrapper::DB_AGENT, Wrapper::DB_PSW, Wrapper::DB_NAME);
$res = mysqli_query($conn, $query);
$id_conv= mysqli_insert_id($conn);
mysqli_free_result($res);
return $id_conv;
}
The Wrapper:: * variables are all set well, in fact, an INSERT operation is done, but not only one! I'm having this situation after I call this function:
This is the content of Conversations table:
And here's the content of Facebook_Info:
What's happening?
I searched and searched...
Then I started to think about what I'm getting here: 2147483647. What represents this number? What's that? Seems like a big number!
And what if my script and my queries were correct but the mistake is the skeleton of my tables?
I must register a 14 digit integer, that is too large for the INT type.
So using BIGINT to store the f_id field made all correct and working!
Hope my mistake helps someone!

Auto primary key insert custom table $wpdb

I need to insert to custom table that has primary key 'id' by number, but I got this problem for example
I have 5605 rows (start with id = 1) in my table,so I have to set current id = 5606 to insert.
1/I set $data['id'] = 5606 by hand to insert it, it works fine. current row with id 5606 is inserted.
but I want it automatically get the right id to insert so I do
2/select * to returns the current number of rows in table, it returns 5604 (always lesser by 1 when I check database has 5605). so I + 2 then do insert.
It ends up insert 3 times like 5606 5607 5608 in my table.
Please help me here is my code
$data = array(
'name' => 'naomi',
'ability' => 'walk',
);
$wpdb->get_results("SELECT * FROM contest");
$numid = $wpdb->num_rows;
$numid +=2;
$data['id'] = $numid;
$wpdb->insert('contest', $data);
The given number is for example, my problem is in that format.
Just declare column id (or whatever you use as primary key) as AUTO_INCREMENT (in MySQL) or SERIAL (in PostgreSQL) and insert all other columns but your primary key.
Example:
CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (ID)
);
INSERT INTO persons (LastName,FirstName,Address,City) VALUES (
'Sample','Person','Sample-street','Sample-city'
);
More than! You should not use any manual inserts for primary keys, because it can make you a lot of problems with handling unsuccessfull queries etc.
SECOND PART. To return number of rows in your table just use
SELECT COUNT(id) FROM persons;

How to avoid duplicate content stored in MySQL database submitted by user

I am creating a site that lets users list up to 5 companies they are associated with. When other users search these companies, all users associated with that company will show up in the search results.
The companies will be submitted by the users through a text input field.
How do I avoid users submitting duplicate companies? E.g. if UserA submits a company called stackoverflow, then UserB comes and also submits stackoverflow, there will be 2 stackoverflows in my database.
I have 3 tables:
Users Table
id|username|email
Company Table
id|company name
UsersCompany Table
id|userID|companyID
I'm using Laravel 5
You should really use Laravel Validation and keyword unique to handle this:
$this->validate($request, [
'company' => 'required|unique:company|max:255'
]);
Also, you could use custom Request class to handle form validation:
public function rules()
{
return [
'company' => 'required|unique|max:255'
];
}
If I were you, I'd use second one.
You can do it with a simple check. If the company does not exists create a new Company or attach the Company to the user.
I assume the companies are submitted in one single text input seperated by commas and you have setup your relations correct. If not check this.
Example:
// Inside your controller
public function post_something(Request $request)
{
// Always good to validate
$this->validate(....);
// Somehow get your user model
$user = ....
// Get companies input and loop
$company_names = $request->input('company_names');
$company_names = explode(',', $company_names );
foreach ($company_names as $company_name)
{
$company = Company::firstOrCreate(['company_name' => $company_name]);
$user->companies()->attach($company);
}
// Your other stuff
//
....
}
This can achieve this by
either creating PRIMARY Key or UNIQUE on company_Name
ALTER TABLE company_Table ADD PRIMARY KEY(company_Name)
ALTER TABLE company_Table ADD UNIQUE (company_Name)
or
IF NOT EXISTS(QUERY) Then INSERT
or
Create BEFORE INSERT trigger .
http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
You can add a unique index on the table. So if your column is named company_name and table is companies you could execute the following:
alter table companies add unique (company_name)
Or alternatively you can do a query in programming before you allow an insert, which checks if the entry already exists. Or a combination of both..
you can use unique key for companies table
http://dev.mysql.com/doc/refman/5.7/en/constraint-primary-key.html
This is very easily obtainable, by using UNIQUE in MySQL.
ALTER TABLE `company_Table` ADD UNIQUE (
`company_Name`
)
By putting this into MySQL, the company_Name column becomes UNIQUE, meaning there can only be one. If you attempt to insert another one, an error returns.
UNIQUE can also be used on member emails on a userbase, if you don't want somebody to log in with the same email. UNIQUE is also perfect for POST_IDs, MEMBER_IDs, COMMENT_IDs, and several others, that could become very useful on a blog, forum, or social media site.
If you would like to create the UNIQUE key upon creating this table, here is how you would do so:
CREATE TABLE Company_Table
(
ID int NOT NULL,
Company_Name varchar(255),
UNIQUE (Company_Name)
)
W3schools has a good tutorial on this: Here
On my opinion you should use Eloquent firstOrCreate method
If you will use this approach, then you even no need any "unique" validation under the companies.
Lets start
DB Schema
users
id
username
companies
id
company_name
user_company
id
user_id
company_id
Models (only relations methods)
User.php
public function companies()
{
return $this->belongsToMany('NAMESPACE_TO_YOUR_MODEL\Company');
/*you can set foreign keys fields if it's not canonical see docs*/
}
Company.php
public function users()
{
return $this->belongsToMany('NAMESPACE_TO_YOUR_MODEL\User');
}
Controller
public function store(CompanyRequest $request)
{
$companiesFromRequest = $request->get('companies');
// it's just for example, you can retreive companies from request with any other approach
$companiesId = [];
foreach ($companiesFromRequest as $company) {
// assumes that $company variable contains all or at least part of info,
// that need to create or identify particuliar comapny
$c = Company::firstOrCreate($company);
$companiesId[] = $c->id;
}
// in this case we just retreive existing user
$user = User::findOrFail($request->get('user_id'));
$user->companies()->sync($companiesId);
// or you can use
// $user->companies()->attach($companiesId);
// difference between this commands you can found in official laravel docs
return redirect('any/place/you/wish')
}
Application Layer:
Use Laravel's Validation property 'unique' to establish that only unique company name is allowed.
public function store(Request $request)
{
$this->validate($request, [
'company_name' => 'required|unique:companies|max:255',
]);
// The company name is valid, store in database...
}
Database Layer:
add a constraint as unique to the migration of the company's table for company_name column.
$table->string('company_name')->unique();
found the answer in larval i just use firstOrNew() Creation Method
will attempt to locate a record in the database matching the given attributes. However, if a model is not found, a new model instance will be returned. Note that the model returned by firstOrNew has not yet been persisted to the database. You will need to call save manually to persist it:
as stated here
Try to build schema as following to get optimum performance.This structure helps you to avoid duplicate data and also do code validations in Laravel to avoid malicious inputs.
CREATE TABLE IF NOT EXISTS `Users Table` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `Company Table` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`company name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `company name` (`company name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `Users Company Table` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`userID` int(11) unsigned NOT NULL,
`companyID` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `userID` (`userID`),
KEY `companyID` (`companyID`),
CONSTRAINT `Users Company Table_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `Users Table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `Users Company Table_ibfk_2` FOREIGN KEY (`companyID`) REFERENCES `Company Table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
I hope this code helps you. This is a sql code to insert unique data
INSERT INTO UsersCompanyTable (userID, companyID)
SELECT * FROM (SELECT $_POST("user_id"), $_POST("company_id")) AS tmp
WHERE NOT EXISTS (
SELECT companyID FROM UsersCompanyTable WHERE companyID = $_POST("company_id")
) LIMIT 1;
You should allow user to select company, and then simply give reference of UserID, CompanyID tables in UsersCompany Table. So you will always have a unique record if UserA insert StackOverFlow and UserB also insert StackOverFlow, in your database. It will be like:
1-UserA-StackOverflow
2-UserB-StackOverFlow.
Or, if you want user to enter the company, check if the same company exists or not:
var checkcompany= "select * from Company Table where company name=company name"
Than check
if(checkcompany ! =null)
Than insert the record or else ignore it.

MySQL INSERT IGNORE Adding 1 to Non-Indexed column

I'm building a small report in a PHP while loop.
The query I'm running inside the while() loop is this:
INSERT IGNORE INTO `tbl_reporting` SET datesubmitted = '2015-05-26', submissiontype = 'email', outcome = 0, totalcount = totalcount+1
I'm expecting the totalcount column to increment every time the query is run.
But the number stays at 1.
The UNIQUE index composes the first 3 columns.
Here's the Table Schema:
CREATE TABLE `tbl_reporting` (
`datesubmitted` date NOT NULL,
`submissiontype` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`outcome` tinyint(1) unsigned NOT NULL DEFAULT '0',
`totalcount` mediumint(5) unsigned NOT NULL DEFAULT '0',
UNIQUE KEY `datesubmitted` (`datesubmitted`,`submissiontype`,`outcome`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
When I modify the query into a regular UPDATE statement:
UPDATE `tbl_reporting` SET totalcount = totalcount+1 WHERE datesubmitted = '2015-05-26' AND submissiontype = 'email' AND outcome = 1
...it works.
Does INSERT IGNORE not allow adding numbers? Or is my original query malformed?
I'd like to use the INSERT IGNORE, otherwise I'll have to query for the original record first, then insert, then eventually update.
Think of what you're doing:
INSERT .... totalcount=totalcount+1
To calculate totalcount+1, the DB has to retrieve the current value of totalcount... which doesn't exist yet, because you're CREATING a new record, and there is NO existing data to retrieve the "old" value from.
e.g. you're trying eat your cake before you ever went to the store to buy the ingredients, let alone mix/bake them.

php can't create new database tables via query

Here is the code that should create 2 new tables in MySQL if they do not exist CMS and PAGES, however what is occurring is that CMS is being created but PAGES is ignored and is not created.
Here is the php function responsible for creating tables
private function buildDB() {
#lets create cms table if one does not exist
$make_cms = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS cms (
title VARCHAR(150),
bodytext TEXT,
date VARCHAR(100)
)
MySQL_QUERY;
return mysql_query($make_cms);
#lets create pages table if one does not exist
$make_pages = <<<MySQL_QUERY2
CREATE TABLE IF NOT EXISTS pages (
pid int NOT NULL AUTO_INCREMENT, PRIMARY KEY (pid),
title VARCHAR(150),
text TEXT,
date VARCHAR(100)
)
MySQL_QUERY2;
return mysql_query($make_pages);
}
And there ya go that's the function. I ones again will note part one of it works so there for $make_cms does its job and actually makes a table called CMS while the function $make_pages does nothing and fails to create PAGES table.
You're returning from the function before beginning the second CREATE TABLE statement. You'll therefore never reach the second statement.
// Don't return here!
return mysql_query($make_cms);
Instead assign the value and return only if FALSE:
$success_cms = mysql_query($make_cms);
// Return only on error of first table creation
if (!$success) {
return FALSE;
}
// Then create the second table...
If you use echo mysql_error() to output the error, it will tell you. I would guess it is because you have a field named text, which is a reserved word. Try escaping your field names in tics ( ` ) or avoiding reserved words.
#lets create pages table if one does not exist
$make_pages = <<<MySQL_QUERY2
CREATE TABLE IF NOT EXISTS pages (
`pid` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`pid`),
`title` VARCHAR(150),
`text` TEXT,
`date` VARCHAR(100)
)
MySQL_QUERY2;
The query itself seems OK, so one explanation may be that either pages exists already, or the user somehow has no permission to create that particular table.
You can modify your code so it shows what the MySQL server said when running the query:
result = mysql_query($make_pages);
if (!$result) {
echo('Invalid query: ' . mysql_error());
}
change "text TEXT," to text1 TEXT and it should work! remember sql is not case sensitive.

Categories