Pergunta

I am currently trying to solve a problem which I just noticed since the property didn't actually have a main_image allocated to it.

But as soon as I allocated a main image to it. The Related images caused problems.

Property table has a belongsTo relation

public $belongsTo = array( 'Image' => array(
            'className' => 'Image',
            'foreignKey' => 'main_image_id'
        ));

and also a hasMany

public $hasMany = array(
        'Image' => array(
            'className' => 'Image',
            'foreignKey' => 'property_id'
        )
    );

the problem is when I am populating the related images I am receiving multiple errors similar to the below

Illegal string offset 'id'

my data is like so

property(array)
    Image(array)
        id1
        descriptionPainted Brick Design
        imageuploads/properties/3/Amazing-Painted-Brick-Houses-Design.jpeg
        property_id3
        0(array)
            id1
            descriptionPainted Brick Design
            imageuploads/properties/3/Amazing-Painted-Brick-Houses-Design.jpeg
            property_id3

and the view was generated like so

<div class="related">
    <h3><?php echo __('Related Images'); ?></h3>
    <?php if (!empty($property['Image'])): ?>
    <table cellpadding = "0" cellspacing = "0">
    <tr>
        <th><?php echo __('Id'); ?></th>
        <th><?php echo __('Description'); ?></th>
        <th><?php echo __('Image'); ?></th>
        <th><?php echo __('Property Id'); ?></th>
        <th class="actions"><?php echo __('Actions'); ?></th>
    </tr>
    <?php foreach ($property['Image'] as $image): ?>
        <tr>
            <td><?php echo $image['id']; ?></td>
            <td><?php echo $image['description']; ?></td>
            <td><?php echo $image['image']; ?></td>
            <td><?php echo $image['property_id']; ?></td>
            <td class="actions">
                <?php echo $this->Html->link(__('View'), array('controller' => 'images', 'action' => 'view', $image['id'])); ?>
                <?php echo $this->Html->link(__('Edit'), array('controller' => 'images', 'action' => 'edit', $image['id'])); ?>
                <?php echo $this->Form->postLink(__('Delete'), array('controller' => 'images', 'action' => 'delete', $image['id']), null, __('Are you sure you want to delete # %s?', $image['id'])); ?>
            </td>
        </tr>
    <?php endforeach; ?>
    </table>
<?php endif; ?>

    <div class="actions">
        <ul>
            <li><?php echo $this->Html->link(__('New Image'), array('controller' => 'images', 'action' => 'add')); ?> </li>
        </ul>
    </div>
</div>

I am not sure if maybe the relations are setup incorrectly or that maybe I would have to include an if to see if there is data before the array and then implement a for loop.

Foi útil?

Solução

I will answer it rather then comment. You will get all the properties and their main image like this (place this in your controller action eg. index()):

$properties= $this->Property->find('all', array(
    'contain' => array(
        'Image' => array(
            'conditions' => array(
                'main_image' => true
            )
        )
    )
));

$this->set('properties', $properties);

and in your view:

<img src='<?php echo '/realestateagencyadministration/img/' . $property['Image'][0]['main_image'] ?>' style='max-width: 100%;height:150px;;'>

Don't take my for sure on this last one, I am wrinting this out of my head, I think you need [0], but you can easily fix that.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top