how to show 2 form from same table in search - yii

I want to ask, how to show 2 forms from the same table with a related table in yii2,
example
<?php
$label1 = app\models\AppFieldConfigSearch::getLabelName(Location::tableName(), "Location");
?>
<?= $form->field($model, 'id_location')->label($label1) ?>
<?php
$label1 = app\models\AppFieldConfigSearch::getLabelName(LocationUnit::tableName(), "label1");
?>
<?= $form->field($model, 'label1')->label($label1) ?>
<?php $dataListAssetMaster2 = ArrayHelper::map(Owner::find()->asArray()->all(), 'id_owner', 'name');
echo $form->field($model, 'id_owner')->widget(Select2::classname(), [
'data' => $dataListAssetMaster2,
'pluginOptions' => [
'allowClear' => true
],
'options' => [
'prompt' => 'Pilih Nama']
])->label("Name");
?>
<?php $dataListAssetMaster4 = ArrayHelper::map(Location::find()->asArray()->all(), 'id_location', 'address');
echo $form->field($model, 'id_location')->widget(Select2::classname(), [
'data' => $dataListAssetMaster4,
'pluginOptions' => [
'allowClear' => true
],
'options' => [
'prompt' => 'Status Pembebasan']
])->label("location address");
// my search model
public function rules()
{
return [
[['id_location_unit', 'id_location', 'id_owner','id_mst_status1'], 'integer'],
[['label1', 'label2', 'label3', 'keterangan1', 'keterangan2', 'keterangan3','address'], 'safe'],
];
}
I want to display the same 2 tables on 1 search form, but I have a problem when I use the code, one of the forms does not appear

You can combine 2 of your model/form into 1 search model. Let your search model decide on the search logic.

Related

Drupal create node by post Api call fail with message "Could not determine entity type bundle: \\u0022type\\u0022 field is missing."}

I'm trying to create a node via drupal API but I have this error:
Got error 'PHP message: PHP Fatal error: Uncaught GuzzleHttp\\Exception\\ClientException: Client error: `POST https://site.it/entity/node?_format=hal_json` resulted in a `422 Unprocessable Entity` response:\n{"message":"Could not determine entity type bundle: \\u0022type\\u0022 field is missing."}
this is my function:
public function createFaq($notes, $telegram_id){
$url = "/entity/node?_format=hal_json";
$opt = [
'headers' => self::$baseHeader,
'body' => json_encode([
[
'type' => [ ['target_id' => 'faq'] ],
'title' => 'title',
'utente' => [ [ 'target_id' => '123462' ] ],
'field_domanda' => [ [ 'value' => $notes['domanda'] ] ],
'field_presenza' => [ [ 'value' => $notes['presenza'] == "Si"? true : false ] ],
]
])
];
$response = $this->client->request('POST', $url , $opt);
$r = json_decode( $response->getBody());
return $r;
}
But i't really strange because this other function is working
public static function createUser($title){
$url= "/entity/node?_format=hal_json";
$opt = [
'headers' => self::$baseHeader,
'body' => json_encode([
'title' => [ [ 'value' => $title ] ],
'type' => [ [ 'target_id' => 'article' ] ],
])
];
$response = $this->client->request('POST', $url , $opt);
$r = json_decode( $response->getBody());
return $r;
}
Can someone understood my error?
This is because the json data are enclosed in square brackets twice, just remove one pair :
$opt = [
'headers' => self::$baseHeader,
'body' => json_encode([
//[
'type' => [ ['target_id' => 'faq'] ],
'title' => 'title',
'utente' => [ [ 'target_id' => '123462' ] ],
'field_domanda' => [ [ 'value' => $notes['domanda'] ] ],
'field_presenza' => [ [ 'value' => $notes['presenza'] == "Si"? true : false ] ],
//]
])
];

How to disable a particular config component on specific action in YII2?

i have a user component in my web config file which is working fine.
$config = [
'components' => [
'user' => [
'class' => 'common\components\User'
],
...
],
];
except on a page where I want to use a different user file. Is it possible to disable the particular component in Yii for a particular action?
There are several ways foe example You could declare two different components
$config = [
'components' => [
'user' => [
'class' => 'common\components\User'
],
'user1' => [
'class' => 'common\components\MyAlternativeUser'
],
...
],
];

How to Alias one of the table sql in cakephp 3.6.3

I'm trying to access Contacts. I'm getting below error
Error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'Contacts'
If you are using SQL keywords as table column names, you can enable identifier quoting for your database connection in config/app.php.
Check SQL Query Screenshot
How do I setAlias() in table associations ?
ContactsController.php
public function index()
{
$this->paginate = [
'contain' => ['Users', 'Contacts', 'SourceProspects', 'Status',
'Secteurs', 'Products']
];
$contacts = $this->paginate($this->Contacts);
$this->set(compact('contacts'));
}
public function view($id = null)
{
$contact = $this->Contacts->get($id, [
'contain' => ['Users', 'Contacts', 'SourceProspects', 'Status',
'Secteurs', 'Products', 'Leads', 'Accounts']
]);
$this->set('contact', $contact);
}
ContactsTable.php
$this->setTable('contacts');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id'
]);
$this->belongsTo('Contacts', [
'foreignKey' => 'contact_type_id'
]);
$this->belongsTo('Leads', [
'foreignKey' => 'lead_id'
]);
$this->belongsTo('SourceProspects', [
'foreignKey' => 'source_prospect_id'
]);
$this->belongsTo('Accounts', [
'foreignKey' => 'account_id'
]);
$this->belongsTo('Status', [
'foreignKey' => 'statut_id'
]);
$this->belongsTo('Secteurs', [
'foreignKey' => 'secteur_id'
]);
$this->belongsTo('Products', [
'foreignKey' => 'product_id'
]);
$this->hasMany('Accounts', [
'foreignKey' => 'contact_id'
]);
$this->hasMany('Leads', [
'foreignKey' => 'contact_id'
]);
}
The problem seems to be in ContractsTable here
$this->belongsTo('Contacts', [
'foreignKey' => 'contact_type_id'
]);
in this way cake join the Contacts table with itself so creating a non unique alias
maybe is just a typo and you wanted to do
$this->belongsTo('ContactTypes', [
'foreignKey' => 'contact_type_id'
]);
but if you actually want to use that relationship then you have to alias the joined Contacts table
$this->belongsTo('ParentContacts', [ // choose your alias here
'className' => 'Contacts'
'foreignKey' => 'contact_type_id'
]);
so every time you have to refer to the joined table you can do something like
'contain' => [
...,
'ParentContacts',
],

How should I send file to api server in laravel by Guzzle Client

Could you please tell me how should I send a file to upload on api?
In api server we have a post method which it gives two parameters:
one name in query and a file in formData then it gives a link as response body.
I'm going to send file via guzzle client in this format:
$file = $request->file('InputFile');
$file_path = $file->getPathname();
$response = $this->CX_Client->post('/file/upload?name='.$fileName, [
'formData' =>
[
'file' =>
[
'name' => 'InputFile',
'contents' => fopen($file_path, 'r'),
'filename' => $fileName
]
]
]);
But this does not work and server can not find its parameters in this request. What's wrong with me?
You should use multipart instead of formData
https://guzzle.readthedocs.io/en/latest/request-options.html#multipart
$response = $this->CX_Client->post('/file/upload?name='.$fileName, [
'multipart' => [
[
'name' => 'foo',
'contents' => 'data',
'headers' => ['X-Baz' => 'bar']
],
[
'name' => 'baz',
'contents' => fopen('/path/to/file', 'r')
],
[
'name' => 'qux',
'contents' => fopen('/path/to/file', 'r'),
'filename' => 'custom_filename.txt'
],
]
]);

Correct JSON ouput iPhone json_encode

I'm trying to get a correct json output for iPhone i'm using the code:
<?php
$post = array('items' => 1, 'title' => message, 'description' => description);
echo json_encode($post);
?>
And the output is:
{"items":1,"title":"message","description":"description"}
But i want a output with [
{
"items": [
{
"title": "message",
"description": "description"
}
]
}
Can someone tell me how to do that?
try
$post = array('items' => array( 0 => array('title' => message, 'description' => description)));
(warning: untested!)