简体   繁体   中英

Joomla Component - Site Model Issue

I've been developing a Real Estate component for several of my clients. I have most everything working, except getting images from a separate table.

One example of a site model is City. Within the city model, the primary get (query) is a list of properties, based on the city. Example, if from either the cities view, or menu item, a city such as Dallas is selected, then in the single city view only property listings where property city = Dallas will show.

That part is working great, as it is supposed to. The problem is that every listing has multiple images, stored in an images table. On the property list view, only a single image (out of potentially many) needs to be pulled an displayed.

Adding as a "linked" sub-query within the main properties query doesn't work, as it will create multiple duplicate property listings for each image. So, I created as a custom method, get image.

The compiler that I am using to aid in the building doesn't support a signature method (method that takes values), so I needed to add as a class value. In the getItems function for properties, I created the class value of:

$this->a_property_id = $item->id;

Then, in the getImage custom function I added

$query->where('a.propid = ' . $db->quote($this->a_property_id));

as part of the query. In theory, this should pull a single item where a.propid equals the property id, such as property id=3, then pull an item from images where propid=3.

Lastly, I added the image code in the site view default.php as part of the foreach:

<?php foreach ($this->items as $item): ?>
<li>
<div class="uk-grid uk-panel uk-panel-box">
<div class="uk-width-medium-1-3">
  <?php if(empty($this->image->path)){ ?>
  <div> <a href="<?php echo 'index.php?option=com_mostwantedrealestate&view=property&id='.$item->id;?>" title="<?php echo $item->name;?>" rel=""> <img class="uk-thumbnail uk-thumbnail-medium" src="<?php echo JURI::root().'media/com_mostwantedrealestate/images/No_image_available.png'; ?>"> </a> </div>
  <?php } else { ?>
  <div> <a href="<?php echo 'index.php?option=com_mostwantedrealestate&view=property&id='.$item->id;?>" title="<?php echo $item->name;?>" rel=""> <img class="uk-thumbnail uk-thumbnail-medium"  src="<?php echo JURI::root() . $this->image->path . $this->image->filename; ?>"></a></div>
  <?php } ?>
</div>
  <div class="uk-width-medium-1-3 uk-float-left">
  <a href="<?php echo 'index.php?option=com_mostwantedrealestate&view=property&id='.$item->id;?>" title="<?php echo $item->name;?>" rel="" >
    <h3><?php echo $item->name; ?></h3>
  </a>
  </div>

This is kind of working, but not completely, so I'm unsure what I may be missing. With the way I have this coded, it's displaying an image. If there is only a single property listing, then it shows the proper image, however if there a, lets say, two properties: id=1 and id=3, then it shows propid=3 from the image table for both listings, rather than showing propid=1 for id=1 and propid=3 for id=3.

Any thoughts on what I might be missing?

You're looping over the $this->items but you use the same $this->image object for all of them. So you see the same image over and over again.

You can fix this by adding the image information to the item. Since you want to show just one image you can easily do this with single query joining your item table and your image table.

Another way is to combine the necessary data from different queries in your model so you can use something like $item->image->path .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM