Optimal sampling from the database - rest

I make a sample around the city, the city has areas, they have complexes, and apartment complexes. In the end, I need to get an apartment for a certain city, indicating the associated filters (district, complex, etc.). At the moment I did this:
public function actionIndex()
{
$cities = Cities::find()->where(['id' => Yii::$app->request->get('city_id')])->with([
'districts' => function ($query){
$query->filterWhere([
'id' => Yii::$app->request->get('district_id'),
]);
},
'districts.complexes' => function ($query) {
$query->filterWhere([
'id' => Yii::$app->request->get('complex_id'),
'type_id' => Yii::$app->request->get('complex_type_id'),
'developer_id' => Yii::$app->request->get('developer_id'),
]);
},
'districts.complexes.apartments' => function ($query) {
$query->filterWhere([
'amount_room' => Yii::$app->request->get('amount_room'),
'yardage' => Yii::$app->request->get('yardage'),
'level' => Yii::$app->request->get('level'),
'price' => Yii::$app->request->get('price'),
]);
},
])->all();
$query = [];
foreach ($cities as $city) {
foreach ($city->districts as $district) {
foreach ($district->complexes as $complex) {
foreach ($complex->apartments as $apartment) {
$query[] = $apartment;
}
}
}
}
return new ArrayDataProvider([
'allModels' => $query,
]);
}
But it looks kind of crooked, maybe I went the wrong way, and can I do it better?
UPD: I did almost like Yasin Patel.
$cities = Cities::find()
//->select('cities.id') // list your attributes comma saperated
->leftJoin('districts','cities.id=districts.city_id') // join districts table
->leftJoin('complex','districts.id=complex.district_id') // join complex table
->leftJoin('apartment','complex.id=apartment.complex_id') // join apartment table
->where(['cities.id' => Yii::$app->request->get('city_id')])
->andWhere(['cities.id' => Yii::$app->request->get('city_id')])
->filterWhere(['districts.id' => Yii::$app->request->get('district_id')])
->filterWhere(['complex.id' => Yii::$app->request->get('complex_id')])
->filterWhere(['complex.type_id' => Yii::$app->request->get('complex_type_id')])
->filterWhere(['complex.developer_id' => Yii::$app->request->get('developer_id')])
->filterWhere(['apartment.amount_room' => Yii::$app->request->get('amount_room')])
->filterWhere(['apartment.yardage' => Yii::$app->request->get('yardage')])
->filterWhere(['apartment.level' => Yii::$app->request->get('level')])
->filterWhere(['apartment.price' => Yii::$app->request->get('price')]);
return new ActiveDataProvider([
'query' => $cities,
]);
Returns:
[
{
"name":"City1",
"region":{
"id":7,
"name":"Region city1."
}
}
]
But how now to choose all the apartments found?

Try this query.
$cities = Cities::find()
->select('city.id') // list your attributes comma saperated
->leftJoin('districts','city.id=districts.city_id') // join districts table
->leftJoin('complexes','districts.id=complexes.districts_id') // join complexes table
->leftJoin('apartments','complexes.id=apartments.complexe_id') // join apartments table
->where(['id' => Yii::$app->request->get('city_id')])
->andWhere(['city.id' => Yii::$app->request->get('district_id')])
->andWhere(['districts.id' => Yii::$app->request->get('district_id')])
->andWhere(['complexes.id' => Yii::$app->request->get('complex_id')])
->andWhere(['complexes.type_id' => Yii::$app->request->get('complex_type_id')])
->andWhere(['complexes.developer_id' => Yii::$app->request->get('developer_id')])
->andWhere(['apartments.amount_room' => Yii::$app->request->get('amount_room')])
->andWhere(['apartments.yardage' => Yii::$app->request->get('yardage')])
->andWhere(['apartments.level' => Yii::$app->request->get('level')])
->andWhere(['apartments.price' => Yii::$app->request->get('price')])
->asArray();
Use your table name and attributes names as needed.
Than use this query in ArrayDataProvider.
return new ArrayDataProvider([
'allModels' => $query,
]);
This will return you records as array.

It turned out to optimize the code in this way::
public function actionIndex()
{
$query = Apartment::find()
->joinWith('complex')
->joinWith('complex.district')
->joinWith('complex.district.city')
->where(['cities.id' => Yii::$app->request->get('city_id')])
->filterWhere(['districts.id' => Yii::$app->request->get('district_id')])
->filterWhere(['complex.id' => Yii::$app->request->get('complex_id')])
->filterWhere(['complex.type_id' => Yii::$app->request->get('complex_type_id')])
->filterWhere(['complex.developer_id' => Yii::$app->request->get('developer_id')])
->filterWhere(['apartment.amount_room' => Yii::$app->request->get('amount_room')])
->filterWhere(['apartment.yardage' => Yii::$app->request->get('yardage')])
->filterWhere(['apartment.level' => Yii::$app->request->get('level')])
->filterWhere(['apartment.price' => Yii::$app->request->get('price')]);
return new ActiveDataProvider([
'query' => $query,
]);
}
Thanks to everyone for their help.

Related

Perl how to access a value of nested hash without specific some middle keys (wildcard)

I trying to fetch the facebook mobile posts.
$VAR1 = {
"mf_story_key" => "225164133113094",
"page_id" => "102820022014173",
"page_insights" => {
"102820022014173" => {
"actor_id" => "102820022014173",
"page_id" => "102820022014173",
"page_id_type" => "page",
"post_context" => {
"publish_time" => "1641702174",
"story_name" => "EntStatusCreationStory"
},
"psn" => "EntStatusCreationStory",
"role" => 1,
}
},
"story_attachment_style" => "album",
};
$publish_time = $VAR1->{page_insights}{102820022014173}{post_context}{publish_time};
If 102820022014173 is a dynamic value, how do I access the publish_time value without specific it?
You need to get the keys to the page_insights hash and then iterate through them.
use strict;
use warnings;
use 5.010;
my $post = {
"mf_story_key" => "225164133113094",
"page_id" => "102820022014173",
"page_insights" => {
"102820022014173" => {
"actor_id" => "102820022014173",
"page_id" => "102820022014173",
"page_id_type" => "page",
"post_context" => {
"publish_time" => "1641702174",
"story_name" => "EntStatusCreationStory"
},
"psn" => "EntStatusCreationStory",
"role" => 1,
}
},
"story_attachment_style" => "album",
};
my $insights = $post->{page_insights};
my #insight_ids = keys %{$insights};
for my $id ( #insight_ids ) {
say "ID $id was published at ",
$insights->{$id}{post_context}{publish_time};
}
gives
ID 102820022014173 was published at 1641702174
for my $page_insight ( values( %{ $VAR1->{page_insights} } ) ) {
my $publish_time = $page_insight->{post_context}{publish_time};
...
}
If there's always going to exactly one element,
my $page_insight = ( values( %{ $VAR1->{page_insights} } )[0];
my $publish_time = $page_insight->{post_context}{publish_time};
...
(You can combine the two statements if you so desire.)

is it possible to return different models in an API response

This is my first time posting here so please pardon my errors:
I have a search functionality whose route is:
Route::get('/search', 'SearchController#index');
Currently, I have an eloquent relationship where products has many deals. is it possible to return a single level deep array doing the following:
If the product has an active deal, return the deal only;
Otherwise, return the product itself.
here's what I earlier implemented in my Product.php:
public function deals()
{
return $this->hasMany(Deal::class, 'product_id');
}
Deal.php
public function product()
{
return $this->hasOne(Product::class, 'id', 'product_id');
}
SearchController:
public function index(Request $request)
{
$per_page = $request->per_page ?? 10;
$products = Product::query()->latest()
->when($request->query('filter'), function ($query) use ($request) {
$query->with('deals')->where('title', 'LIKE', "%$request->filter%");
})
->when($request->query('category'), function ($query) use ($request) {
$query->with('deals')->whereHas('categories', function ($q) use ($request) {
$q->where('title', 'LIKE', "%$request->category%");
});
})
->paginate($per_page);
return new PaginatedCollection($products, ProductResource::class);
}
and in my ProductResource:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
public function toArray($request)
{
$details = array_filter($this->details ?: [], function ($d) {
return $d != "";
});
$personalizedOptions = array_filter($this->personalized_options ?: [], function ($o) {
return $o != "";
});
return [
'id' => $this->id,
'createdAt' => $this->created_at,
'updatedAt' => $this->updated_at,
'title' => $this->title,
'sellerId' => $this->sellerId,
'description' => $this->description,
'categories' => CategoryResource::collection($this->categories),
'details' => $details,
'active' => (bool) $this->active,
'defaultPreviewImageId' => $this->default_preview_image_id,
'originalPrice' => $this->originalPrice,
'shippingPrice' => $this->shippingPrice,
'shippingWeightLbs' => $this->shippingWeightLbs,
'shippingWeightOz' => $this->shippingWeightOz,
'shippingMaxDays' => $this->shipping_max_days,
'shippingMinDays' => $this->shipping_min_days,
'personalized' => (bool) $this->personalized,
'personalizedOptions' => $personalizedOptions,
'deals' => $this->deals ?? null,
'options' => ProductOptionResource::collection($this->productOptions),
'images' => ImageResource::collection($this->images->whereNull('meta')),
'preview' => new ImageResource($this->images->where('meta', '=', 'preview')->first()),
];
}
}
Now, I have refactored the ProductResource to this but it's all returning null response
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
public function toArray($request)
{
$details = array_filter($this->details ?: [], function ($d) {
return $d != "";
});
$personalizedOptions = array_filter($this->personalized_options ?: [], function ($o) {
return $o != "";
});
if($this->deals){
DealResource::collection($this->deals);
}else{
return [
'id' => $this->id,
'createdAt' => $this->created_at,
'updatedAt' => $this->updated_at,
'title' => $this->title,
'sellerId' => $this->sellerId,
'description' => $this->description,
'categories' => CategoryResource::collection($this->categories),
'details' => $details,
'active' => (bool) $this->active,
'defaultPreviewImageId' => $this->default_preview_image_id,
'originalPrice' => $this->originalPrice,
'shippingPrice' => $this->shippingPrice,
'shippingWeightLbs' => $this->shippingWeightLbs,
'shippingWeightOz' => $this->shippingWeightOz,
'shippingMaxDays' => $this->shipping_max_days,
'shippingMinDays' => $this->shipping_min_days,
'personalized' => (bool) $this->personalized,
'personalizedOptions' => $personalizedOptions,
// 'deals' => $this->deals ?? null,
'options' => ProductOptionResource::collection($this->productOptions),
'images' => ImageResource::collection($this->images->whereNull('meta')),
'preview' => new ImageResource($this->images->where('meta', '=', 'preview')->first()),
];
}
}
}
The reason why it may be giving the null result because of the condition check. it is returning an array you need to update it to this.
if(count($this->deals))
this will check if the deal array contains an element in the array. if not it will return products.

Muliple Dropdown Select option in Drupal8 Custom Form

I have to design the form with following fields
Country
State
District
If you select Country the Country value will be loaded in the Country Dropdown Field
Then if you select State the States belongs to Countries value will be loaded in the State Dropdown.
After that If you select District the Districts value belongs to States will be loaded in the District Dropdown field.
I have tried but not work properly
$form['Country'] = [
'#type' => 'select',
'#title' => t('Country'),
'#description' => t('Country'),
'#required' => TRUE,
'#options' => $countries,
// '#default_value' => setInConfigandGEtVAlue,
'#ajax' => ['callback' => [$this, 'getStates'], 'event' => 'change',
'method' => 'html',
'wrapper' => 'states-to-update',
'progress' => [
'type' => 'throbber',
'message' => NULL,
],
],
];
$states = [];
// if($default_country != "") { load default states of selected country by get the default va
lue of country (setInConfigandGEtVAlue)
// $states = $this->getStatesByCountry($default_country);
// }
$form['state'] = array(
'#title' => t('State'),
'#type' => 'select',
'#description' => t('Select the state'),
'#options' => $states,
// '#default_value' => setInConfigandGEtVAlue,
'#attributes' => ["id" => 'states-to-update'],
'#multiple' => TRUE,
'#validated' => TRUE
);
$district_opt_records = [];
$form['districts'] = array(
'#type' => 'select',
'#title' => t('District Name'),
'#options' => $district_opt_records,
'#required' => TRUE,
'#attributes' => ["id" => 'district-to-update'],
'#validated' => TRUE,);
public function getStates(array &$element, FormStateInterface $form_state) {
$triggeringElement = $form_state->getTriggeringElement();
$value = $triggeringElement['#value'];
$states = $this->getStatesByCountry($value);
$wrapper_id = $triggeringElement["#ajax"]["wrapper"];
$renderedField = '';
foreach ($states as $key => $value) {
$renderedField .= "<option value='".$key."'>".$value."</option>";
}
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand("#".$wrapper_id, $renderedField));
return $response;
}
public function getStatesByCountry($default_country) {
//add you logic return states by country
return $states;
}
The above code working only Countries and State but not able to get District values

Drupal programatically create a form with a for condition

I'm trying to create a form to create a person, and this person can have some relation with another one (like a wife, husband, child, ..), so I try to create something with a button to add a relation, here is my code :
if (empty($form_state['number_liaisons'])) {
$form_state['number_liaisons'] = 1;
}
$form['info_contact']['liaison'] = array(
"#type" => "fieldset",
"#title" => "Liaisons",
"#attributes" => array("class" => array("center"))
);
for ($i = 1; $i <= $form_state['number_liaisons']; $i++) {
$form['info_contact']['liaison'][$i] = array(
"#type" => "fieldset",
"#title" => "Liaison",
"#attributes" => array("class" => array("center"))
);
$form['info_contact']['liaison'][$i]['contact'] = array(
'#type' => 'textfield',
'#title' => t("Personne a lier"),
'#autocomplete_path' => 'crm/autocomplete_liaison',
);
$form['info_contact']['liaison'][$i]['type'] = array(
'#type' => 'select',
'#title' => t("Type de liaison"),
'#options' => Array('enfant' => 'Enfant', 'conjoint' => 'conjoint'),
'#empty_option' => t('- Choisir un type de liaison -'),
);
}
$form['info_contact']['liaison']['add_item'] = array(
'#type' => 'submit',
'#value' => t('Add liaison'),
'#submit' => array('liaison_add_item'),
'#limit_validation_errors' => array(),
);
if ($form_state['number_liaisons'] > 1) {
$form['info_contact']['liaison'] = array(
'#type' => 'submit',
'#value' => t('Remove liaison'),
'#submit' => array('liaison_remove_item'),
'#limit_validation_errors' => array(),
);
}
And the two methods to add / remove a relation :
function liaison_add_item($form, &$form_state)
{
$form_state['number_liaisons']++;
$form_state['rebuild'] = true;
}
function liaison_remove_item($form, &$form_state)
{
if ($form_state['number_liaisons'] > 1) {
$form_state['number_liaisons']--;
}
$form_state['rebuild'] = true;
}
My problem is that when I click on the "Add liaison" button all the fields disappear, and when I click on the remove liaison the fields came back.
So when I have more than 1 in my $form_state['number_liaisons'] the for doesn't show my fields.
Anyone know how to fix that?
I think it's because the liaisons all share the same field name, the $form_state doesn't really care if they are nested. So try renaming to something like;
["contact_{$i}"] rather than just ['contact']

Magento 2 Plugins / Interceptors accessing and modifying $this object

I have a plugin that i want to modify functionality of a method within specific class in Magento 2 however am not quite sure on how to access the original object and return the modified data.
Original Method
protected function _initTotals()
{
$source = $this->getSource();
$this->_totals = [];
$this->_totals['subtotal'] = new \Magento\Framework\DataObject(
['code' => 'subtotal', 'value' => $source->getSubtotal(), 'label' => __('Subtotal')]
);
/**
* Add shipping
*/
if (!$source->getIsVirtual() && ((double)$source->getShippingAmount() || $source->getShippingDescription())) {
$this->_totals['shipping'] = new \Magento\Framework\DataObject(
[
'code' => 'shipping',
'field' => 'shipping_amount',
'value' => $this->getSource()->getShippingAmount(),
'label' => __('Shipping & Handling'),
]
);
}
/**
* Add discount
*/
if ((double)$this->getSource()->getDiscountAmount()) {
if ($this->getSource()->getDiscountDescription()) {
$discountLabel = __('Discount (%1)', $source->getDiscountDescription());
} else {
$discountLabel = __('Discount');
}
$this->_totals['discount'] = new \Magento\Framework\DataObject(
[
'code' => 'discount',
'field' => 'discount_amount',
'value' => $source->getDiscountAmount(),
'label' => $discountLabel,
]
);
}
$this->_totals['grand_total'] = new \Magento\Framework\DataObject(
[
'code' => 'grand_total',
'field' => 'grand_total',
'strong' => true,
'value' => $source->getGrandTotal(),
'label' => __('Grand Total'),
]
);
/**
* Base grandtotal
*/
if ($this->getOrder()->isCurrencyDifferent()) {
$this->_totals['base_grandtotal'] = new \Magento\Framework\DataObject(
[
'code' => 'base_grandtotal',
'value' => $this->getOrder()->formatBasePrice($source->getBaseGrandTotal()),
'label' => __('Grand Total to be Charged'),
'is_formated' => true,
]
);
}
return $this;
}
This i have set to have a plugin to modify functionality of method above with di.xml:
<type name="Magento\Sales\Block\Order\Totals">
<plugin disabled="false" name="Harrigo_EverDiscountLabel_Plugin_Magento_Sales_Block_Order_Totals" sortOrder="10" type="Harrigo\EverDiscountLabel\Plugin\Magento\Sales\Block\Order\Totals"/>
</type>
Plugin
class Totals
{
public function after_initTotals(
\Magento\Sales\Block\Order\Totals $subject,
$result
) {
if ((double)$subject->getSource()->getDiscountAmount() != 0 OR $subject->getSource()->getDiscountDescription() != null) {
if ($subject->getSource()->getDiscountDescription()) {
$discountLabel = __('Offer (%1)', $source->getDiscountDescription());
} else {
$discountLabel = __('Offer');
}
$subject->_totals['discount'] = new \Magento\Framework\DataObject(
[
'code' => 'discount',
'field' => 'discount_amount',
'value' => $source->getDiscountAmount(),
'label' => $discountLabel,
]
);
}
return $subject;
}
}
Have used $subject instead of $this within the plugin, this does not work for me however. How do I access the $this object within the plugin to add / overwrite $this->_totals['discount'] and return the updated $this object from within the plugin. I have it working fine with a standard preference but would rather use a plugin if possible.
I think you should check this before implementing above code.
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
As per devdocs for Magento2 protected functions can not be intercepted so We can not use plugins for that.
May be that is causing issue in your case.
Hope this helps!