Deactivate the ID (older Table)? - eloquent

I have an old table without an ID (without a primary key).
Now I have to insert a record via Laravel and I get an error message:
Error Code : 904 Error Message : ORA-00904: "ID": invalid ID position.
How can I deactivate the ID?
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Log extends Model
{
use HasFactory;
protected $connection = 'oracle_client_nonprefix';
protected $table = 'al_logs';
}

Use any of your existing columns as primary key and disable auto increment
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Log extends Model
{
use HasFactory;
protected $primaryKey = 'log_date';
protected $incrementing = false;
}

Related

How to join these two tables to get results as api resource?

am having two tables one is the main client table and the other one is a sub-client table, The sub-clients is a client of the main client that is the primary key of the main client is in sub-client table how can i join these two tables and take the output in controller to pass over the resource as JSON for API ??
This is my model of the main client :
namespace App;
use Illuminate\Database\Eloquent\Model;
class Clients extends Model
{
//
}
This is my Controller of the main client :
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Clients;
use App\Http\Resources\Client as ClientResource;
// use Illuminate\Http\Response;
class Clients_controller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//get clients
$clients = Clients::paginate(15);
//Return collection of clients as a resource
return ClientResource::collection($clients);
}
}
This is my model of the Sub client :
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sub_clients extends Model
{
//
}
This is my Controller of the sub-client :
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Sub_clients;
use App\Http\Resources\Sub_client as SubclientResource;
class Sub_client extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//get Sub_clients
$subclients = Sub_clients::paginate(15);
//Return collection of sub clients as a resource
return SubclientResource::collection($subclients);
}
}
Can anyone please help since am new to laravel
I joined using Query Builder
$client = DB::table('clients')
->join('sub_clients', 'clients.c_id', '=', 'sub_clients.c_id')
->select('clients.*', 'sub_clients.password', 'sub_clients.username as name')
->where('sub_clients.c_id','=',$c_id)
->get();

extending model : Access parent attribute from the child

have to accessing parent attribute from child
after instanciate objectTwo, $object_two->some_objectone_field get null value instead of expected value
below both objects structure :
object one
<?php namespace Username\Plugin\Models;
use Model;
class ObjectOne extends Model
{
use \October\Rain\Database\Traits\Validation;
public $rules = [
....
];
public $table = 'username_plugin_objectone';
public function __construct()
{
parent::__construct();
}
}
object two
<?php namespace Username\Plugin\Models;
use Username\Plugin\Models\ObjectOne;
class ObjectTwo extends ObjectOne
{
public $rules = [
....
];
public $table = 'username_plugin_objecttwo';
public function __construct()
{
parent::__construct();
}
}
database tables
username_plugin_objectone table :
id
some_objectone_field
...
username_plugin_objecttwo table:
id
objectone_id
some_objecttwo_field
...
what am i going wrong ? thanks by advance
ok, things are more clear, i can't access parent properties from database because the parent $table property is overload by child $table property ...

SYMFONY custom CONSTRAINT -> Pass variable to a custom CONSTRAINT / How CONSTRAINT binded to a form field can OVERRIDE CONSTRAINT in ANNOTATION

My goal: I built a custom constraint in SYMFONY, I needed to pass a variable to that constraint.
The context: The constraint do a check if a value is unique in the DB, if it is not, it raises a CONSTRAINT alert. That works alright when the FORM is used to create a new tuple in the DB but if it is an edit it raises an exception which should be bypass by checking that the value already existing, exists for the tuple Id being edited.
Hence I needed to pass the Id of the tuple being edited to my constraint check.
At first I implemented my custom constraint in my entity:
class MyEntity{
/**
* #MyBundleAssert\CheckValueAlreadyInDB(
* message = "already_exists_in_db",
* fieldToSearch = "my_value",
* tableToSearch = "my_table"
*)
*/
private myValue;
}
As one can see, I did not find a way to implement a way to pass a VARIABLE using the constraint with ANNOTATION. By searching, I understood I could do that by using the __construct() of my custom constraint class:
/**
* #Annotation
*/
class CheckValueAlreadyInDB extends Constraint{
public $message;
public $fieldToSearch;
public $tableToSearch;
public $idToCheck;
public $idToCheckFieldName;
public function __construct($options){
if(count($options)>0){
$this->idToCheck = $options['idToCheck'];
$this->idToCheckFieldName = $options['idToCheckFieldName'];
$this->fieldToSearch = $options['fieldToSearch'];
$this->tableToSearch = $options['tableToSearch'];
$this->message = $options['message'];
}
}
public function validatedBy()
{
return 'validator_check_value_already_in_db';
}
}
And, the ConstraintValidator extended class linked to it:
class CheckValueAlreadyInDBValidator extends ConstraintValidator
{
private $con;
public function __construct($con){
$this->con = $con;
}
public function validate($value, Constraint $constraint)
{
////My stuff to get a record from the DB////
$sel = new PdoSelect($this->con);
$search = $sel->returnRecordsInTableForSpecificKey([$constraint->fieldToSearch],[$value], $constraint->tableToSearch,false);
//////////////////////////////////////////////
$sameId = false;
if($constraint->idToCheck!==null){
$idToCheckInRetrieveRecord = $search->{$constraint->idToCheckFieldName};
$sameId = ($idToCheckInRetrieveRecord==$constraint->idToCheck)?true:false;
}
if($search!=null&&!$sameId){
$this->context->buildViolation($constraint->message)
->setParameter('%string%', $value)
->addViolation();
}
}
}
With service:
validator.unique.check_value_already_in_db:
class: MyBundle\Form\CustomConstraints\CheckValueAlreadyInDBValidator
arguments: ['#doctrine.dbal.default_connection']
tags:
- { name: validator.constraint_validator, alias: validator_check_value_already_in_db }
I my FORM (AbstractType extended class) for the field regarding myValue, I did edit the constraints attribute.
class MyEntityType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
....
$builder->add('myValue',****Type::class,array(
'constraints' => array(
new CheckValueAlreadyInDB(array(
'idToCheck'=>$options['data']->getId(),
'idToCheckFieldName'=>'id',
'fieldToSearch'=>'my_value',
'tableToSearch'=>'my_table',
'message' => "value_already_exists_in_db"))
)
));
...
}
}
I thought that the CONSTRAINT defined in the buildForm() would override the one defined in the * #MyBundleAssert\CheckValueAlreadyInDB(..) of MyEntity class (which should be the default behaviour). But It did not! I had to delete the ANNOTATION above MyEntity to make the constraint work as defined in the buildForm().
Does anyone know if there is a setting that could permit to have a constraint in a buildForm() overriding one existing as an ANNOTATION in MyEntity, but still let the ANNOTATION above a field in MyEntity be the default behavior? Or is there is a way to pass VARIABLE to ANNOTATIONS?
I found the solution.
My mistake was to try to use constraints in class MyEntityType extends AbstractType:
$builder->add('myValue',****Type::class,array(
'constraints' => array(
new CheckValueAlreadyInDB(array(
'idToCheck'=>$options['data']->getId(),
'idToCheckFieldName'=>'id',
'fieldToSearch'=>'my_value',
'tableToSearch'=>'my_table',
'message' => "value_already_exists_in_db"))
)
));
Update:
DON'T USE IT HERE
Have a look at class-constraint-validator section in the doc.
Implement the ConstraintValidator extended class above the class of the Entity where the validator has to execute its check and not above one attribute of the Entity class. That way one can have access to other attributes of the entity and use it as conditionals in the ConstraintValidator extended class.

Error: Table social_profiles for model SocialProfile was not found in datasource default

I am trying to get social logins to work using hybridauth
my app/Model/SocialProfile.php looks like
<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
class SocialProfile extends AppModel {
public $belongsTo = 'User';
}
my app/Model/User.php looks like
<?php
App::uses('AppModel', 'Model');
class User extends AppModel {
public $hasMany = array(
'SocialProfile' => array(
'className' => 'SocialProfile',
)
);
....
I am getting this error:
Error: Table social_profiles for model SocialProfile was not found in datasource default.
Thank you for you assitance
If social_profiles table is present in your database, not require to
define in model. But in your case its not working. so you need to
alternative process in CakePHP.
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
class SocialProfile extends AppModel {
public $useTable = 'social_profile ';
// This model uses a database table 'social_profile'
public $name = 'SocialProfile ';
// If you do not specify it in your model file it will be set to the
// class name by constructor.
}
I hope the above model will be work for you perfectly. I have found your solution from read this Docs

No adapter for type Zend_Db_Table_Row error?

I have a project in which I use more than one adapter.
So In ma models i created an abstract model
abstract My_Config1_Model extends Zend_Db_Table_Abstract
{
public function init()
{
$db = Zend_Registry::get('dbcon')->getDb(Kiga_Data_Database::MASTER);
$this->setDefaultAdapter($db);
}
}
and then I inherit this abstaract class like:
class MyModel extends My_Config1_Model
{
protected $_name = 'mytable';
protected $_primary = 'id';
protected $_rowClass = 'MyRow';
}
class MyRow extends Zend_Db_Table_Row_Abstract
{
}
and the in my controller I try:
$table = new MyModel();
when I fetch alll it works:
$results = $table->fetchAll(); // works fine
but when I try to filter it it does not work:
results = $table->fetchRow("id = 1"); // Does not work. I get the error Error: No adapter for type MyRow.
Anybody any Idea?
Thanks.
I forgot I use also paginator
$paginator = Zend_Paginator::factory($results);
That's not the place you should set the Db adapter for this table.
The init() method is called after the table class has parsed its options and set up the adapter for the table. So all you've accomplished is to set the default Db adapter for subsequent table construction, but it has no effect on the current table if you do this in the init() method.
Consider this simplified example:
class MyTable
{
static $defaultDb;
protected $db;
static function setDefaultDb($db) { self::$defaultDb = $db; }
public function __construct() {
$this->db = self::$defaultDb;
$this->init();
}
public function init() {
// Unfortunately, PHP allows you to run static methods
// as if they are non-static methods, which is confusing.
$this->setDefaultDb($globalDb);
}
}
This example is a simplified model of the way Zend_Db_Table constructs. Note that the init() method sets the class default Db, but this is run after the constructor has already set the instance Db to be the class default Db. So setting the class default Db has no effect.
There are several ways you can set the Db adapter for a table:
For all tables, using the static method setDefaultAdapter(). The intended way to use setDefaultAdapter() is as follows:
Zend_Db_Table_Abstract::setDefaultAdapter($db);
// now all tables will use $db by default
$table = new MyModel();
As a constructor argument:
$table = new MyModel(array('db'=>$db));
You might also be able to use the setOptions() method after the table class has been instantiated.
$table = new MyModel(); // uses default Db
$table->setOptions(array('db'=>$otherDb));
But be aware that the table reads its metadata from the default Db during construction, so if you change the adapter subsequently, the table should be defined identically in both databases.