doublesecretagency / craft-upvote

Upvote plugin for Craft CMS
Other
16 stars 6 forks source link

Clear old data #7

Open jeffaglenn opened 5 years ago

jeffaglenn commented 5 years ago

I have Upvote working great as a "favorite" feature for users on my site. However, when I have an entry favorited then delete the same entry Upvote is still looking for the entry and throwing a Impossible to access an attribute ("fieldName") on a null variable. even though all remaining entires have content in the field. I can see in the upvote_userhistories table that deleted entry id is still there. Is there any way to delete this old data or have Upvote rebuild that table?

lindseydiloreto commented 5 years ago

Hi @jeffaglenn,

That definitely shouldn't be happening. Even if we can't delete the data, it should be able to skip over missing entries gracefully.

Can you provide me with the specific steps to reproduce? And/or, could you send me the full stack trace when you're seeing that error message?

Thanks!

jeffaglenn commented 5 years ago

I was thinking about this and think I have found the issue and need a little guidance.

craft.upvote.userHistory() returns an array of element ids and I'm wanting to access fields within those elements. Here is my template code. I'm setting an entry variable to craft.entries.id(key) so it's looking for the element id when the element is not there. Is there a better way to set this up?

{% set votes = craft.upvote.userHistory() %}

    {% if votes | length %}
        <div class="favorites">
            <div class="grid grid--cards">
            {% for key,value in votes %}

                {% set entry = craft.entries.id(key).with(['coverImage']).one() %}

                <div class="card card--image">

                    {% set image = craft.imager.transformImage(entry.coverImage[0], {
                        width: 300,
                        ratio: 1/1,
                        mode: 'crop',
                        format: 'jpg',
                        interlace: true,
                        position: entry.coverImage[0].focalPoint
                    }) %}
                    <img src="{{ image.url }}" {{ render.altText(entry.coverImage[0]) }}>

                    <div class="card--image--content text--center">
                        <h2>{{ entry.title }}</h2>
                        <div class="card--content--links">
                            <div class="grid grid--half ">
                                <a href="{{ entry.url }}">View</a>
                                {{ craft.upvote.upvote(entry.id) }}
                            </div>
                        </div>
                    </div>
                </div>

            {% endfor %}
            </div>
        </div>
    {% else %}
        <div class="flex flex--center">
            <div>
                <svg class="icon--svg"><use href="#icon-wedding-dress" xlink:href="#icon-wedding-dress"/></svg>
                <h3>No styles found in your Favorites.</h3>
                <a href="{{ siteUrl }}" class="btn">Add favorites</a>
            </div>
        </div>
    {% endif %}
jeffaglenn commented 5 years ago

Think I have it working. I wrapped all of my code in a conditional after I set the entry variable.

{% if votes | length %}
  {% for key,value in votes %}
    {% set entry = craft.entries.id(key).with(['coverImage']).one() %}
    {% if entry %}
      // rest of code
    {% endif %}
  {% endfor %}
{% endif %}
jeffaglenn commented 5 years ago

Well, the above isn't perfect because votes | length will always have something in it since the record isn't deleted from the upvote_userhistories table. Still working...