SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘proposal_db.userlogs’ doesn’t exist - cakephp-3.x

I’m doing some customization inside the CakeDC/users plug-in. I created a table with name “user_logs” which consist of foreign key relationship with the actual “users” table provided by CakeDC/users.
I baked the “user_logs” model using command:
bin\cake bake model UserLogs --plugin CakeDC/Users
After user gets login I’m just generating log transaction inside the “user_logs” table. I added the following line inside the “/vendor/cakedc/users/src/Controller/Traits/LoginTrait.php” file under _afterIdentifyUser function:
$this->activity_log(‘Login’, ‘Login’, $user[‘id’]);
And activity_log function is added inside the src/Controller/AppController.php file:
function activity_log($page, $action, $id=null){
$this->loadModel(‘CakeDC/Users.Userlogs’);
$dataUserLog = $this->Userlogs->newEntity();
$dataUserLog['user_id'] = $this->request->session()->read('Auth.User.id');
if(!empty($id)){
$dataUserLog['reference_id'] = $id;
} else {
$dataUserLog['reference_id'] = 0;
}
$dataUserLog['activity_timestamp'] = date('Y-m-d H:i:s');
$dataUserLog['page'] = $page;
$dataUserLog['action'] = $action;
$this->Userlogs->save($dataUserLog);
}
vendor/cakedc/users/src/Model/Entity/UserLog.php file code:
namespace CakeDC\Users\Model\Entity;
use Cake\ORM\Entity;
class UserLog extends Entity
{
protected $_accessible = [
‘user_id’ => true,
‘reference_id’ => true,
‘activity_timestamp’ => true,
‘page’ => true,
‘action’ => true,
‘user’ => true
];
}
vendor/cakedc/users/src/Model/Table/UserLogsTable.php file code:
namespace CakeDC\Users\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class UserLogsTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('user_logs');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'className' => 'CakeDC/Users.Users'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->dateTime('activity_timestamp')
->allowEmptyDateTime('activity_timestamp');
$validator
->scalar('page')
->maxLength('page', 255)
->allowEmptyString('page');
$validator
->scalar('action')
->maxLength('action', 255)
->allowEmptyString('action');
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['user_id'], 'Users'));
return $rules;
}
}
The surprise part is! this works on localhost but when I’m uploading code on a server it’s not working. On localhost I’ve PHP v7.3.4 and on server I’ve PHP v5.6.40. Can any one suggest what’s wrong with this why it’s working on localhost and not on server? Everything is same I’ve done almost everything cleared model cache on server as well but no luck. Please help.
Not really sure why CAKEPHP is looking for table “proposal_db.userlogs” on server whereas I created “user_logs” table on both local and server. Please suggest?

Related

How to check data exists in the database

I have a function to add new property. But i want to check for duplicate data at column "code" before add new data into database. If data exists will appear a message error.
function addPro(Request $req)
{
$id = $req->type_id;
$type = AssetType::find($id);
if($req->save == 'save'){
$pro = new TypeProperties;
$pro->name = $req->name;
$pro->code = $req->code;
$pro->type = $req->type;
$pro->assettype_id = $req->type_id;
$pro->save();
Schema::table($type->code, function ($table) use ($pro) {
if ($pro->type == "textbox")
$table->string($pro->code )->nullable();
if ($pro->type == "textarea")
$table->text($pro->code )->nullable();
});
return redirect(url($type->id.'/add/property'))->with('message','Save successful');
}
return redirect(url('asset/type/'.$type->id));
}
You can use laravel Request Validation
function addPro(Request $req)
{
$id = $req->type_id;
$type = AssetType::find($id);
if($req->save == 'save'){
$req->validate([
'code' => 'required|unique:tablename'
]);
$pro = new TypeProperties;
$pro->name = $req->name;
$pro->code = $req->code;
$pro->type = $req->type;
$pro->assettype_id = $req->type_id;
$pro->save();
Schema::table($type->code, function ($table) use ($pro) {
if ($pro->type == "textbox")
$table->string($pro->code )->nullable();
if ($pro->type == "textarea")
$table->text($pro->code )->nullable();
});
return redirect(url($type->id.'/add/property'))->with('message','Save successful');
}
return redirect(url('asset/type/'.$type->id));
}
The most simple way to do this is by checking if code is_null :
if (is_null($pro->code)) {
// It does not exist
} else {
// It exists
}
The other way is to make a validation using Laravel's built in ValidateRequest class. The most simple use-case for this validation, is to call it directly in your store() method like this:
$this->validate($req, [
'code' => 'required|unique,
//... and so on
], $this->messages);
With this, you're validating users $req by saying that specified columns are required and that they need to be unique, in order for validation to pass. In your controller, you can also create messages function to display error messages, if the condition isn't met:
private $messages = [
'code.required' => 'Code is required',
'code.unique' => 'Code already exists',
//... and so on
];
You can also achieve this by creating a new custom validation class:
php artisan make:request StorePro
The generated class will be placed in the app/Http/Requests directory. Now, you can add a few validation rules to the rules method:
public function rules()
{
return [
'code' => 'required|unique,
//... and so on
];
}
All you need to do now is type-hint the request on your controller method. The incoming form request is validated before the controller method is called, meaning you do not need to clutter your controller with any validation logic:
public function store(StorePro $req)
{
// The incoming request is valid...
// Retrieve the validated input data...
$validated = $req->validated();
}
If you have any additional question about this, feel free to ask. Source: Laravel official documentation.
What does your migration look like for AssetType?
I ask because you can do this in the schema with ->unique() added to the column on the creation or make a migration to add the constraint.
You can also check with something like this:
// Search database table for entry
$entry = AssetType::where('code', '=', $pro->code)->first();
// If not found
if ($entry === null) {
// Save method here.
}
Otherwise, you can use the manual validator or create a Request with validation
References:
https://laravel.com/docs/5.8/queries#where-clauses
https://laravel.com/docs/5.8/validation#creating-form-requests
https://laravel.com/docs/5.8/validation#manually-creating-validators

Laravel Map DB Column Names Using Proper Convention to Actual DB Column Names in Model

We're building a portal to replace part of an existing application as step one, but the DB schema holds to absolutely no conventions. Aside from the lack of any constraints, indexes, etc the names of columns are not descriptive and not snake-cased.
Is it possible to map DB table column names so that the portal uses proper descriptive and snake-cased column names like first_name but writes to the actual database column first to at least have the portal be a first step towards cleaning up the tech debt?
For example, similar to how the table name (Model::table) can be set if the table name doesn't follow convention:
Example
private $columns = [
// convention => actual
'first_name' => 'first',
'last_name' => 'last',
'mobile_phone' => 'phone',
'home_phone' => 'otherPhone', // seriously!?
];
I've looked through Model and the HasAttributes trait, but I'm still hoping that this might exist, or someone has found a way to do this as a temporary solution.
You can create a parent class for all your models:
abstract class Model extends \Illuminate\Database\Eloquent\Model {
protected $columns = [];
public function attributesToArray()
{
$attributes = parent::attributesToArray();
foreach ($this->columns as $convention => $actual) {
if (array_key_exists($actual, $attributes)) {
$attributes[$convention] = $attributes[$actual];
unset($attributes[$actual]);
}
}
return $attributes;
}
public function getAttribute($key)
{
if (array_key_exists($key, $this->columns)) {
$key = $this->columns[$key];
}
return parent::getAttributeValue($key);
}
public function setAttribute($key, $value)
{
if (array_key_exists($key, $this->columns)) {
$key = $this->columns[$key];
}
return parent::setAttribute($key, $value);
}
}
Then override $columns in your models:
protected $columns = [
'first_name' => 'first',
'last_name' => 'last',
'mobile_phone' => 'phone',
'home_phone' => 'otherPhone',
];
The proper way is to use accessors and mutators.
Defining An Accessor
public function getFirstNameAttribute() {
return $this->first;
}
Then, you can access the value by $model->first_name.
Defining A Mutator
public function setFirstNameAttribute($value) {
$this->attributes['first'] = $value;
}
Then, you can mutate the value for example:
$model->first_name = 'first_name';
$model->save();

Eloquent does not add foreign key from relation

I could not find an answer to my problem and I hope I can describe it properly. I do hope to be able to provide all necessary information.
Bottom line: Why does the relations parent ID not get injected on creating a new database entry through the parent model.
I have an Occasions model which holds a collection of pictures. Within the addPicture ($name, $filepath) method the exception is thrown. As pointed out rhough code-comments
Occasion.php
// namespace + use directives omitted
class Occasion extends Model
{
use Sluggable;
protected $fillable = [ 'name', 'root-folder', 'path' ];
public function sluggable ()
{
return [
'slug' => [
'source' => [ 'root-folder', 'name', ],
],
];
}
public function pictures ()
{
return $this->hasMany(picture::class);
}
public function addPicture ($name, $filepath)
{
$thumbname = $this->getThumbFilename($filepath);
dump($this,$this->pictures()); // dump to check my data
$pic = Picture::create(compact('name', 'thumbname'));
// this line is never reached
$pic->createThumb($filepath);
}
...
}
Picture.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\Glide\GlideImage;
class Picture extends Model
{
protected $fillable = [ 'name', 'thumbname' ];
public function createThumb ($filepath)
{
$this->ppath = storage_path('app') . "/" . $filepath;
$this->tpath = storage_path('app/public/thumbs') . "/" . $this->getfilename($filepath);
GlideImage::create($this->ppath)->modify([ 'w' => 100, 'h' => 100, 'fit' => 'max' ])->save($this->tpath);
$this->save();
}
public function occasion ()
{
return $this->belongsTo(Occasion::class);
}
/* public function slideshow ()
{
return $this->belongsToMany(Slideshow::class);
}*/
private function getfilename ($path)
{
$tmp = array_slice(explode('/', $path), -3);
return str_replace(" ", "-", implode("-", $tmp));
}
}
The result of dump($this->pictures()); shows the relation and the columns used:
HasMany {#206 ▼
#foreignKey: "pictures.occasion_id"
#localKey: "id"
#query: Builder {#205 ▶}
#parent: Occasion {#215 ▶}
#related: Picture {#210 ▶}
}
But I'm getting an error message telling me that my occasion_id (in pictures table) is missing a default value. Looking at the built query the occasion_id is indeed missing. What I can't figure out is why said ID does not get injected as I am creating the new picture instance through an occasion-object.
QueryException
SQLSTATE[HY000]: General error: 1364 Field 'occasion_id' doesn't have a default value (SQL: insert into `pictures` (`name`, `thumbname`, `updated_at`, `created_at`) values (IMG_0015.JPG, 2006-PVanlage-06-IMG_0015.JPG, 2017-09-12 19:34:07, 2017-09-12 19:34:07))
I hope that all necessary information is provided.
First you need to add "occasion_id" to fillable array in App\Picture model. Secondly, you need to create occasion object first, then pass the ID addPicture to create picture object, see below
public Picture extends Model{
public $fillable = ['name', 'filepath', 'occasion_id'];
public function occasion(){
$this->belongsTo(App\Occassion::class);
}
}
public function addPicture ($name, $filepath, $occasion_id)
{
$thumbname = $this->getThumbFilename($filepath);
dump($this,$this->pictures()); // dump to check my data
$pic = Picture::create(compact('name', 'thumbname', 'occasion_id'));
// this line is never reached
$pic->createThumb($filepath);
}
There's a smarter way to do this, but this should work.

URL friendly in routes

I'm creating url friendly in my app, but it's not working, the app is giving me some issues related with "-".
It's giving me an error of:
ErrorException in PostController.php line 60:
Trying to get property of non-object
My ideal URL is:
http://domain.com/CATEGORY-title-of-post-ID
My route is:
Route::get('{category}-{title}-{id}', 'PostController#show');
PostController show function:
public function show($category,$title,$id)
{
$post = Post::find($id);
$user = Auth::user();
$comments = Comment::where('post_id',$id)
->where('approved',1)
->get();
return view('posts.show',compact('post','comments','user'));
}
Blade View:
<?php
$title_seo = str_slug($feature->title, '-');
?>
<a href="{{url($feature->categories[0]->internal_name."-".$title_seo."-".$feature->id)}}" rel="bookmark">
...</a>
There's a library called Eloquent-Sluggable that will create a unique slug for each post and correctly URL encode it.
To install (taken from the docs):
composer require cviebrock/eloquent-sluggable:^4.1
Then, update config/app.php by adding an entry for the service provider.
'providers' => [
// ...
Cviebrock\EloquentSluggable\ServiceProvider::class,
];
Finally, from the command line again, publish the default configuration file:
php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"
To use, add the Sluggable trait to your model:
use Cviebrock\EloquentSluggable\Sluggable;
class Post extends Model
{
use Sluggable;
/**
* Return the sluggable configuration array for this model.
*
* #return array
*/
public function sluggable()
{
return [
'slug' => [
'source' => 'title'
]
];
}
}
When you save an instance of your model, the library will automatically create a slug and save it to the newly created slug column of your model's table. So to access the slug you'd use $model->slug
To achieve your desired slug, rather than create it from title set by default. You can pass the source attribute of the sluggable method an array of field names, using a dot notation to access the attributes of a related model, like so:
public function sluggable()
{
return [
'slug' => [
'source' => ['category.name','title','id']
]
];
}
}
Why are you genering your "friendly URL" manually?
You have route helper function that builds for you a URL based on the given parameters.
Route::get('{category}-{title}-{id}', [
'as => 'post.show',
'uses' => 'PostController#show'
]);
echo route('post.show', ['testing', 'title', 'id']); // http://domain.dev/testing-title-id
This is not the best approach to implement SEO friendly URLs, anyway.
In your controller you ALWAYS use your ID to find a post, that means that category and title are completely useless to determine which resource needs to be served to the user.
You can make your life easier by doing something like:
Route::get('{id}-{slug}', [
'as => 'post.show',
'uses' => 'PostController#show'
]);
echo route('post.show', ['id', 'slug']); // http://domain.dev/id-slug
In your model you create an helper function that generates the slug for your post:
class Post
{
[...]
public function slug()
{
return str_slug("{$this->category}-{$this->title}");
}
}
Then, in your controller you need to check that the slug used to access the article is correct or not, since you don't want Google to index post with wrong slugs. You essentially force a URL to be in a certain way, and you don't lose index points.
class PostController
{
[...]
public function show($id, $slug)
{
$post = Post::findOrFail($id);
$user = Auth::user();
if ($post->slug() !== $slug) {
return redirect()->route('posts.show', ['id' => 1, 'slug' => $post->slug()]);
}
$comments = Comment::where('post_id', $id)->where('approved', 1)->get();
return view('posts.show', compact('post', 'comments', 'user'));
}
}

Custom Validation connected to database working on localhost but not on remote sever

I have created MY_Form_validation that extends CI_Form_validation. I have loaded the library in Customer Controller. The validate_email function in customer model checks whether the customer exists while logging in. It is working fine on localhost but the method always returns false on the remote server even when a user actually exists. What could be wrong?
I have loaded the library like this
$this->load->library('form_validation');
Login email validation rules
$validate_data = array(
array(
'field' => 'login_email',
'label' => 'Email',
'rules' => 'trim|required|validate_email|is_active|xss_clean'
)
);
My_Form_validation library
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
public function __construct(){
parent::__construct();
}
public function validate_email($field){
$this->CI->load->model('customer_model');
$email = $this->CI->customer_model->validate_email($field);
if($email === true){
return true;
}else{
$this->set_message('validate_email','The {field} does not exist');
return false;
}
}//end method validate_email
}
?>
Validate email model function
public function validate_email($email){
$sql = "SELECT `customer_id` FROM `customer` WHERE `email` = ? AND `status` != -1";
$query = $this->db->query($sql,array($email));
return ($query->num_rows() == 1) ? true : false;
}//end method validate_email
I like using -1 to represent deleted accounts in my databases
I am also autoloading my database
I had the a similar problem. According to the database configurations in codeigniter database.php, I noticed the following
char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
So i dropped my database and recreated it. Changed my database collation to utf8_general_ci and changed my tables charset from armscii8 to utf8 and everything worked
By trusting your words about the data that is currently on your database your function should work, but I would advise you to change the model validate function to something more similiar to CodeIgniter approach.
public function validate_email($email)
{
$this->db->where('email', $email)
->where('status !=', '-1');
$query = $this->db->get('customer');
if($query->num_rows() > 0)
return TRUE;
}
Secondly, create a function to retrieve all the results according to your where condition.
public function getAllEmailsByEmail($email)
{
$this->db->where('email', $email)
->where('status !=', '-1');
return $this->db->get('customer')->result();
}
And just check out many rows it's returned.
print_r($this->CI->customer_model->getAllEmailsByEmail($field));

Resources