I am trying to add reset function to codeigniter's migration. Below is my code:
class Migration extends Backend_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('migration');
}
public function index()
{
//...
}
public function reset()
{
$this->migration->version(1);
$this->db->truncate('ci_sessions');
$this->migration->current();
}
}
It returns error:
Fatal error: Cannot redeclare class Migration_Create_geo_data in D:\web_projects\vProject\framework\application\migrations\002_create_geo_data.php on line 44
If I run them seperately, all are okay. When together it gives error. Any idea?
Most likely, this error is a result of setting your migrations to create tables if they do not already exist AND cache data not being updated right away.
Your Migration script calls the DB_forge::create_table method which takes two parameters. Parameter one is the table name and parameter two is optional. It is the if_not_exists flag. The default value is false however; if it is set to true tables will only be created if they do not already exist.
If your tables are created with the if_not_exists parameter set to false the caching issue will (probably) never happen:
$this->dbforge->create_table('table_name');
If tables are created with the if_not_exists parameter set to true, you will need to force the cache to update when reloading your migrations.
$this->dbforge->create_table('table_name', TRUE);
Here are a couple options to avoid this issue:
Only send the table name as a parameter to the create_table method
Unset data_cache['table_names'] after the migration->version(0) call
If you choose option 2, here is a method that works:
public function reset() {
$this->load->library('migration');
if (!$this->migration->version(0)) {
echo $this->migration->error_string();
}
// unset table cache - this will force it to update
unset($this->db->data_cache['table_names']);
if (!$this->migration->current()) {
echo $this->migration->error_string();
}
}
In addition to this, the migration files are autoloaded and saved in the session.
I changed this line in system/libraries/Migration.php: include $f[0]; to include_once $f[0];
Most likely, you made a migration by copy/pasting from an earlier one & now have two migration files with the same class declared
ie,
class Migration_Add_blog extends CI_Migration
in two files
Related
I have a custom service provider in which I am accessing a model in boot(). But when I run php artisan migrate, it shows the below error:
[Illuminate\Database\QueryException] SQLSTATE[42S02]: Base table or view not found: 1146 Table '********' doesn't exist
I found that if we add if (!app()->runningInConsole()) { inside boot(), it works successfully.
This is the code we have used in the service provider:
public function boot()
{
$this->bindCurrentPartToNav();
}
private function bindCurrentPartToNav(): void
{
$currentPartName = \App\Http\Helpers\Part::getPartName();
view()->composer(
'includes.partials.part',
function ($view) use ($currentPartName) {
$view->with('currentPartName', $currentPartName);
}
);
}
Helper file:
public static function getPartName(): ?string
{
return PartModel::PartKey()->active()->pluck('name')->first();
}
Model:
public function scopePartKey($query): Builder
{
return $query->where('identifier', config('env.PART_KEY'));
}
Is there any way to remove that service provider from php artisan migrate so that we can remove runningInConsole() check in each refresh?
Thanks for your help in advance.
As any environment configuration, in your case a general configuration, you should assign a default value fall back.
public static function getSectionName(): ?string
{
try {
return SectionModel::sectionKey()->active()->pluck('name')->first();
} catch (\Exception $e) {
return null;
}
}
This will simulate the case where the section model with that specific identification_key is missing in the database.
This will also prevent any issues with the initial migration.
But in the end, you tied a model with a view rendering code. You should find another solution to dissociate them. For example, you can move the boot() code out of the model and link it to a middleware.
You can also use Singleton pattern (since it's like a general unique config across the application)
In Laravel 7 I did the following:
php artisan make:controller ClientGroupController --resource --model=ClientGroup
I modified the edit function in ClientGroupController to look as follows:
public function edit(ClientGroup $clientGroup)
{
return view('extranet.groups.create_modal_form', compact('clientGroup'));
}
I also added this route resource: Route::resource('groups', 'ClientGroupController');
A dd($clientGroup) in the view (when visiting the page http://127.0.0.1:8000/groups/2/edit) yields none of the data for the current record (a blank ClientGroup object).
Did I miss a step? Why does $clientGroup->id return null in my view (id is the primary key of the client_group table).
public function edit($id)
{
$clientGroup = ClientGroup::find($id);
return view('extranet.groups.create_modal_form', compact('clientGroup'));
}
I had a similar problem with resource routes defined like this:
Route::resource('others', OtherServiceController::class);
But my model name was OtherService
This was my edit and update function
public function edit(OtherService $otherService)
{
return view('others.master-edit', compact('otherService'));
}
public function update(OtherServiceRequest $request, OtherService $otherService)
{
$otherService->update($request->validated());
return redirect()->route('others.index')->withToastSuccess('Success Update Data');
}
But this code threw errors about a missing required parameter, because resource name and model name are not the same. So, my solution was to change the resource routes to match the model.
Route::resource('otherService', OtherServiceController::class);
Maybe you can read this question, marked answer tells about overriding
I want to assign a default value in Laravel model but the value should come from the config file.
I am writing below code but its giving me error
protected $attributes = [
'is_generation' => true,
'price' => config('test.MY_PRICE'),
];
it's showing me an error like Constant expression contains invalid operations
How can I get value from config file and set in $attribute variable in Laravel model file?
You can use the saving model event by adding this code to your Eloquent Model:
protected static function boot()
{
// sets default values on saving
static::saving(function ($model) {
$model->price = $model->price ?? config('test.MY_PRICE');
});
parent::boot();
}
With this code in place, if the field price is null, it will have assigned a value from the config key just a moment before saving the Model in the database.
BTW you can change the check like if it's an empty string or less then a number and so on, this is only an example.
Class member variables are called "properties". You may also see them
referred to using other terms such as "attributes" or "fields", but
for the purposes of this reference we will use "properties". They are
defined by using one of the keywords public, protected, or private,
followed by a normal variable declaration. This declaration may
include an initialization, but this initialization must be a constant
value--that is, it must be able to be evaluated at compile time and
must not depend on run-time information in order to be evaluated.
The only way you can make this work is :-
<?php
namespace App;
class Amazon
{
protected $serviceURL;
public function __construct()
{
$this->serviceURL = config('api.amazon.service_url');
}
}
You can use attribute mutator as explained here: https://laravel.com/docs/5.8/eloquent-mutators#defining-a-mutator
Class Example{
public function setPriceAttribute(){
return $this->attributes['price'] = config('test.MY_PRICE');
}
}
In a Laravel job I have:
use Spatie\Valuestore\Valuestore;
and
public function __construct()
{
$this->settings = Valuestore::make(storage_path('app/settings.json'));
}
and
public function handle()
{
if($this->settings->get('foo') == 'test') {
etc...
and on this I get an error Undefined property App\Jobs\MyJobName::$settings. What is going wrong?
Even if I do this:
public function handle()
{
$this->settings = Valuestore::make(storage_path('app/settings.json'));
if($this->settings->get('foo') == 'test') {
etc...
I get the same error.
Update based on the comments
MyJobName is called in a custom artisan command, that happens to also use Valuestore but I assume that would unrelated.
In the class CustomCommand:
use Spatie\Valuestore\Valuestore;
and
public function __construct()
{
parent::__construct();
$this->settings = Valuestore::make(storage_path('app/settings.json'));
}
and
public function handle()
{
if($this->settings->get('foo') == 'test') // This works in this custom command!
{
$controller = new MyController;
MyJobName::dispatch($controller);
}
}
So in CustomCommand I use Valuestore in exactly the same way as in MyJobName but in the latter it doesn't work.
As per one of the comments: I do not make $this->settings global as I don't do that in CustomCommand either and it works fine there.
Update 2
If I add protected $settings; above the __construct() function as per the comments it still doesn't work, same error.
Just declare the settings property as public in your Job Class.
public $settings;
public function __construct()
{
$this->settings = Valuestore::make(storage_path('app/settings.json'));
}
I recently had this error. I've tried to make the variables public, delete all the variable inside the Jobs class and even rename and delete the class itself. But it didn't work.
Shortly, I run this artisan command php artisan optimize:clear to clear all the caches, views, routes, etc. And it somehow solve the problem about variable in my problem. For anyone who is still have this OP's problem, give a try to my solution above.
If you use JOB by QUEUE, you need all the requests or SQL queries to do by the method handle
public function handle()
{
$this->settings = Valuestore::make(storage_path('app/settings.json'));
....
}
Because the constructor works when you make the object of class, and this object is serialized and stored in the database and after the unserialization and the handle is triggered.
You may need to restart your queue worker
From Laravel documentation
Remember, queue workers are long-lived processes and store the booted application state in memory. As a result, they will not notice changes in your code base after they have been started. So, during your deployment process, be sure to restart your queue workers.
If you use a daemon php artisan queue:restart
If you use queue:work on your bash hit Ctrl+C then again php artisan queue:work should be enough
Hy!
I'm new to the Doctrine library. I'm trying to take advantage of the Doctrine Behavior (Templating) system to build a Authenticable behavior but I ran into a few problems.
This is how i wanted to declare it:
class BaseAdminUser extends Doctrine_Record{
public function setTableDefinition(){
// ...
}
public function setUp(){
// ...
$this->actAs(new Doctrine_Template_Authenticatable());
}
}
This is how i wanted to use it:
In my admin bootstrap file:
// ...
$admin = new AdminUser();
if(!$admin->login_check()){
redirect("login.php");
}else{
// $admin->id is available
}
In my admin login.php page:
// ...
if($data = $_POST){
$admin->login($data);
}
I wanted my template to use Native PHP Sessions (using $_SESSION) to do the following:
upon login set a session and save the record Doctrine_Core::HYDRATE_ARRAY in it
upon instantiation of Record and Template check if the session data exists
and load the invoking Record with the id from the session
One approach i tried was to re-route the $invoker. That doesn't work.
class Doctrine_Template_Authenticatable extends Doctrine_Template{
// ...
public function setUp(){
// ...
if(isset($session_data['id'])){
$invoker = &$this->getInvoker();
$invoker = $this->getTable()->find($session_data['id']);
}
}
// ...
}
I've looked trough the Doctrine_Record API Documentation and i could not find a function which would load (hydrate) a different table row into a object based on id or otherwise.
There's fromArray($arr) and hydrate($arr, $overwrite):
Doctrine_Record::fromArray(string array, bool deep))
Doctrine_Record::hydrate(array data, boolean overwriteLocalChanges)
But that is fake and would just load the data without the id.
$this->getInvoker()->hydrate($session_data);
// (...)
echo $admin->id; // empty
$this->getInvoker()->fromArray($session_data);
// Uncaught exception 'Doctrine_Table_Exception' with message 'Unknown relation alias id'
My question is:
Is there a way overwrite or load data into the invoking Doctrine_Record from a Doctrine_Template ?