craft-upvote icon indicating copy to clipboard operation
craft-upvote copied to clipboard

Clear old data

Open jeffaglenn opened this issue 6 years ago • 4 comments

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?

jeffaglenn avatar Nov 28 '18 03:11 jeffaglenn

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!

lindseydiloreto avatar Nov 28 '18 06:11 lindseydiloreto

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 avatar Nov 28 '18 16:11 jeffaglenn

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 avatar Nov 28 '18 17:11 jeffaglenn

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...

jeffaglenn avatar Nov 28 '18 17:11 jeffaglenn